I’m new to wordpress and also working with WP REST API for mobile application development for the wordpress website. Here I want get data’s filter & search based post meta custom fields.

I’ve tried for this example but getting all results not belongs property_featured=1

Kindly find my postmeta table structure for example.

meta id post id meta key           meta value 
---------------------------------------------
2548    1000    property_featured    
3068    1078    property_featured   1   
3619    1124    property_featured   1 

Here i want to get the post based on property_featured=1 only. Pls help to me fix on this.

1 Answer
1

You will need to add custom query vars:

add_filter('rest_query_vars', 'wpse225850_add_rest_query_vars');

function wpse225850_add_rest_query_vars($query_vars) {

    $query_vars = array_merge( $query_vars, array('meta_key', 'meta_value', 'meta_compare') );

    return $query_vars;

}

Now, get your posts at example.com/wp-json/wp/v2/posts?filter[meta_key]=property_featured&filter[meta_value]=1.

You can follow this ticket for more info.

Leave a Reply

Your email address will not be published. Required fields are marked *