phpwordpressadvanced-custom-fieldswordpress-rest-api

REST filter based on ACF properties does not work


I have this REST data:

[{"id":215,"acf":{"stad":{"value":"barcelona","label":"barcelona"},"description":"","images":[{"ID":191,"id":191,"title":"logo-black.png","filename":"logo-black.png","filesize":3080,"url":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","link":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/logo-black-png\/","alt":"","author":"1","description":"","caption":"","name":"logo-black-png","status":"inherit","uploaded_to":0,"date":"2018-04-16 15:39:37","modified":"2018-08-05 15:19:12","menu_order":0,"mime_type":"image\/png","type":"image","subtype":"png","icon":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-includes\/images\/media\/default.png","width":443,"height":98,"sizes":{"thumbnail":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black-150x98.png","thumbnail-width":150,"thumbnail-height":98,"medium":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black-300x66.png","medium-width":300,"medium-height":66,"medium_large":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","medium_large-width":443,"medium_large-height":98,"large":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","large-width":443,"large-height":98}}]}},{"id":205,"acf":{"stad":{"value":"oslo","label":"oslo"},"description":"","images":false,"myid":"333"}}]

I created a filter to query with the "myid" parameter like this:

/wp-json/wp/v2/hotels/?myid=333

This is the filter code I added to the functions.php:

add_filter('rest_hotels_vars', function ($valid_vars)
{
    return array_merge($valid_vars, array('myid', 'meta_query'));
});


add_filter('rest_hotels_query', function($args, $request) 
{
    $myid = $request->get_param('myid');

    if (!empty( $myid)) 
    {
        $args['meta_query'] = array(
            array(
                'key'     => 'myid',
                'value'   => $myid,
                'compare' => '=',
            )
        );      
    }

    return $args;
}, 10, 2 );

Hotels is a custom post type.

The query always returns all the rows, the filter has no effect.

What's wrong here?


Solution

  • If you look into the filter closely you will understand the error.

    apply_filters( "rest_{$this->post_type}_query", array $args, WP_REST_Request $request );

    This is your code:

    add_filter('rest_hotels_query', function($args, $request){
    
    $myid = $request->get_param('myid');
    
    if (!empty( $myid)) 
    {
        $args['meta_query'] = array(
            array(
                'key'     => 'myid',
                'value'   => $myid,
                'compare' => '=',
            )
        );      
    }
    
    return $args;
    
    }, 10, 2 );
    

    Here rest_hotels_query : we need to put post type name please be careful, if your post type name is hotel then filter should be like : "rest_hotel_query"

    This is the working code:

    add_filter('rest_hotel_query', function($args, $request){
    $myid = $request->get_param('myid');
    
    if (!empty( $myid)) 
    {
        $args['meta_query'] = array(
            array(
                'key'     => 'myid',
                'value'   => $myid,
                'compare' => '=',
            )
        );      
    }
    
    return $args;
    }, 10, 2 );
    

    Same case with the collection parameters for the posts controller:

    apply_filters( "rest_{$this->post_type}_query", array $args, WP_REST_Request $request );

    It should be :

       add_filter('rest_hotel_collection_params', function ($valid_vars){
        return array_merge($valid_vars, array('myid', 'meta_query'));
       });