Using v2 of the REST API, I’m wanting to query some posts by multiple meta keys. With v1 I was able to format the url like &filter[meta_value][month]=12&[meta_value][year]=2015 and it worked (after exposing the meta values to the API).
Now with v2, I can only get this to work by using the methods listed on this GitHub thread: https://github.com/WP-API/WP-API/issues/1599#issuecomment-161166805
Basically, added the meta fields using the rest_query_vars filter like:
add_filter( 'rest_query_vars', 'flux_allow_meta_query' );
function flux_allow_meta_query( $valid_vars )
{
$valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value', 'meta_compare' ) );
return $valid_vars;
}
With that, I can filter by one meta key using a url like wp-json/wp/v2/posts?filter[meta_key]=test&filter[meta_value]=on.
However, it sounds like the only way to filter on multiple meta keys is to write a custom filter. Could someone point me in the right direction of doing that?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Adding a custom endpoint is pretty straightforward.
I also modified the url to look more like
http://example.com/wp-json/namespace/v2/posts?filter[meta_value][month]=12&filter[meta_value][year]=2015
function wp_json_namespace_v2__init()
{
// create json-api endpoint
add_action('rest_api_init', function () {
// http://example.com/wp-json/namespace/v2/posts?filter[meta_value][month]=12&filter[meta_value][year]=2015
register_rest_route('namespace/v2', '/posts', array (
'methods' => 'GET',
'callback' => 'wp_json_namespace_v2__posts',
'permission_callback' => function (WP_REST_Request $request) {
return true;
}
));
});
// handle the request
function wp_json_namespace_v2__posts($request)
{
// json-api params
$parameters = $request->get_query_params();
// default search args
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => -1,
// limit to only ids
// 'fields' => 'ids',
);
// check the query and add valid items
if (isset($parameters['filter']['meta_value'])) {
foreach ($parameters['filter']['meta_value'] as $key => $value) {
switch ($key) {
case 'month':
if (is_numeric($value))
$args['monthnum'] = $value;
break;
case 'year':
if (is_numeric($value))
$args['year'] = $value;
break;
}
}
}
// run query
$posts = get_posts($args);
// return results
$data = array(
'success' => true,
'request' => $parameters,
'count' => count($posts),
'posts' => $posts,
);
return new WP_REST_Response($data, 200);
}
flush_rewrite_rules(true); // FIXME: <------- DONT LEAVE ME HERE
}
add_action('init', 'wp_json_namespace_v2__init');
Method 2
I know this question was solved, but this plugin is out and solved my problem
https://wordpress.org/plugins/wp-rest-filter/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0