Support » Plugin: WP REST API Controller » How to filter by meta

  • I would like to be able to retrieve a Custom Post Type by a particular meta-key’s value.

    I have exposed Custom post type and meta-key via the plugin and it does show up in the response. However, when I try either of the following, I am getting ALL posts (for this custom Post Type) instead of the one I need.

  • I also attempted to add the following code to allow searching by meta_keys:

        add_filter( 'rest_query_vars', 'my_allow_meta_query' );
        function my_allow_meta_query( $valid_vars ) {
            $valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value' ) );
            return $valid_vars;

    I notice that an extra meta node exists on the response. However, the value is null (in the JSON body) and the each meta key/value node looks as if it has been copied to the top level “fields”. So, I would expect that your plugin is basically converting the custom fields to “regular” fields in the response. Following this logic, I would have expected the first rest URL format to work.

    Please help! What am I doing wrong?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor yikesitskevin


    Hi @shannonrl,

    To be honest, I’ve never had success with meta keys and the REST API. The current state of the REST API is very frustrating.

    Our plugin “copies” the meta fields to the normal fields array using the built-in register_rest_field function. These fields are not available for basic querying though (the way a normal field is able to be queried). I’m not sure why.

    Have you had success with retrieving normal posts/pages based on a meta key (using this syntax: filter[meta_key]=metaKeyName&filter[meta_value]=metaKeyValue)? I have had no success.

    The only way I know of working with meta (other than what our plugin does), is to use this plugin from the WP REST API team This lets you do a query like /wp-json/wp/v2/posts/50/meta (where 50 is the ID of a post/page/CPT – yes using the posts endpoints for CPTs). Supposedly you can also query using the filter[meta_key] syntax but with or without the filter function I’ve had no success. Any thing I try returns all the posts; so I’m having the same issue as you.

    And that’s all I know. I’ve done this testing in the past and I just spent another two hours and came away with the same empty handed conclusion 🙁

    Here is an article from a friend of mine who has gone through the same issues:

    Let me know if you find any answers anywhere else; I’d be extremely interested.


    Hi Kevin!

    Thanks for your response on this issue. I thought I was going crazy. lol

    Upon examining the available arguments on the {base-url}/wp-json/{CPT} for the GET Method, I discovered that the filter argument was MISSING!

    Come to find out, the filter argument was removed just prior to WP 4.7 release because it did not line up with the existing endpoint structure. However, later, it was realized just how important the filter argument was to many people, so they provided a plugin to add it back. Since then, they must have changed the query_vars function name to rest_query_vars upstream, but never changed it on the plugin. So, according to this comment, you would need manually update a line to get this plugin to work.

    I will be trying this soon and get back to you on my results.

    OK… I finally got it working by doing the following:
    1. Install and activate plugin: WP-API/rest-filter
    2. Change function name on line 42 (in this plugin) from query_vars to rest_query_vars, per this comment
    3. Add following snippet to your code somewhere:

        * Allow query endpoint by meta-keys
        add_filter( 'rest_query_vars', 'my_allow_meta_query' );
        function my_allow_meta_query( $valid_vars ) {
            $valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value' ) );
            return $valid_vars;
    Plugin Contributor yikesitskevin


    Woohoo! Followed the steps and it works! Thanks for letting me know. I am going to refer people to this thread in the future. Thanks again for doing that research and have a great weekend!

    This is a great help @shannonrl. Thanks for sharing!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to filter by meta’ is closed to new replies.