'WordPress Rest API: Custom filters on custom endpoint

I created a custom endpoint that brings only the fields that I need from each product published on my WordPress page. Each product is a post and belongs to a custom post type (they are not products created with Woocommerce or similar). This custom post type has advanced custom fields like "product_category", "product_brand" and "fuel". I need to filter this endpoint by value and key of those three fields, specifically each of these fields creates an array with different keys and values. The key and value that should be used to filter are slug.

For example, a brand of products is John Deere, so I need to be able to fetch a response from the REST API that only includes products from that brand.

The endpoint has the following path: wp-json/api/v1/products and filters should be added like this: wp-json/api/v1/products?product_category=xxx&product_brand=xxx&fuel=xxx or individually.

Custom endpoint:

function products() {
    $args = [
        'posts_per_page' => -1,
        'post_type' => 'products'
    ];

    $posts = get_posts($args);

    $data = [];
    $i = 0;

    foreach($posts as $post) {
        $data[$i]['product_name'] = $post->post_title;
        $data[$i]['product_category'] = get_field('product_category', $post->ID);
        $data[$i]['product_brand'] = get_field('product_brand', $post->ID);
        $data[$i]['product_specs'] = get_field('product_specs', $post->ID);
        $i++;
    }

    return $data;
}

add_action('rest_api_init', function() {
    register_rest_route('api/v1', 'products', [
        'methods' => 'GET',
        'callback' => 'products',
    ]);
});

Respose:

[
    {
        "product_name": "Grupo Electrógeno de prueba",
        "product_category": {
            "term_id": 13,
            "name": "Grupos electrógenos",
            "slug": "grupos-electrogenos",
            "term_group": 0,
            "term_taxonomy_id": 13,
            "taxonomy": "product_categories",
            "description": "",
            "parent": 0,
            "count": 1,
            "filter": "raw"
        },
        "product_brand": {
            "term_id": 11,
            "name": "John Deere",
            "slug": "john-deere",
            "term_group": 0,
            "term_taxonomy_id": 11,
            "taxonomy": "brands",
            "description": "",
            "parent": 0,
            "count": 1,
            "filter": "raw"
        },
        "product_specs": {
            "fuel": {
                "term_id": 7,
                "name": "Diésel",
                "slug": "diesel",
                "term_group": 0,
                "term_taxonomy_id": 7,
                "taxonomy": "fuels",
                "description": "",
                "parent": 0,
                "count": 1,
                "filter": "raw"
            }
        }
    }
]

I hope someone can help me, thank so much!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source