3

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
  • This is a simple SQL query, you may do it using WP_query. If you're not familiar with SQL, you may find many examples of WP_query... After that, it depends what your parameters are (static, comming from user...)
    – Random
    May 6, 2016 at 13:31

1 Answer 1

3

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.

11
  • 1
    Thanks for your answer. I need to know which file need to add given above code. Is it possible to do multiple custom meta fields like above mentioned answer. May 6, 2016 at 14:18
  • Anyone pls help to me which file i need to add given answer. May 6, 2016 at 14:53
  • in your wordpress plugin
    – Ahed Eid
    May 6, 2016 at 14:58
  • 1
    @shankarmsr Add the code to your functions.php file.
    – wpclevel
    May 6, 2016 at 14:58
  • @Dan Its works fine. But if the meta value was empty means that post also returning with response so this case empty field post are not able to come right. And also is it possible to get filter works for multiple custome fields here May 6, 2016 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.