Support » Fixing WordPress » Query a CPT by meta using the core rest API

  • I am having trouble using the core rest API to do a search using meta queries.

    Before when I was using the plugin, my query looked like this:


    This would give me all my “stores” in the city of Seattle. It was made possible by using this hook:

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

    Really simple stuff. However with the new core, the “filter” parameter has been removed and I can no longer get my stores by meta.

    I tried doing something like this:


    However this does not work. It seems like meta values aren’t included in the query parameters. Is there a hook in place I can use to add this in? I know I can always use the plugin to revert back to using filter but I would rather not rely on the plugin. Anyone know how to query by meta again using the core rest api?

    • This topic was modified 2 years, 7 months ago by  jaspercreel.
    • This topic was modified 2 years, 7 months ago by  jaspercreel.
    • This topic was modified 2 years, 7 months ago by  jaspercreel.
Viewing 3 replies - 1 through 3 (of 3 total)
  • @jaspercreel – the filter parameter was deprecated when the API was merged with core in WordPress 4.7.0. You can re-enable the filter functionality with this plugin:

    I know that, at the bottom of my post I explain that I don’t want to use this plugin. I was able to hook into the old rest API plugin to make this functionality happen. I was hoping there would be some built in functionality for doing meta queries this time, or some kind of hook, instead of having to use a plugin.

    In the REST developer’s handbook it says,

    When the REST API was merged into WordPress core the ?filter query parameter was removed to prevent future compatibility and maintenance issues. The ability to pass arbitrary WP_Query arguments to the API using a ?filter query parameter was necessary at the genesis of the REST API project, but most API response filtering functionality has been superseded by more robust query parameters like ?categories=, ?slug= and ?per_page=.

    First-party query parameters should be used whenever possible. However, the rest-filter plugin restores the ability to pass arbitrary ?filter values in API request if needed.

    I think this documentation really needs to be updated. There are only 3 quick examples of “more robust query parameters,” but no link to a full list of whatever the filter has been replaced with.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Query a CPT by meta using the core rest API’ is closed to new replies.