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:
https://example-site/wp-json/wp/v2/store?filter[meta_key]=city&filter[meta_value]=Seattle
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:
https://example-site/wp-json/wp/v2/store?city=Seattle
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?
- The topic ‘Query a CPT by meta using the core rest API’ is closed to new replies.