The following code should add multiple meta queries capability to all your post types. It supports CPT (Custom Post Type) and ACF (Advanced Custom Field). Source code also available on Github.
Add it to your function.php
add_action( 'rest_api_init', 'wp_rest_filter_add_filters' );
/**
* Add the necessary filter to each post type
**/
function wp_rest_filter_add_filters() {
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
add_filter( 'rest_' . $post_type->name . '_query', 'wp_rest_filter_add_filter_param', 10, 2 );
}
}
/**
* Add the filter parameter
*
* @param array $args The query arguments.
* @param WP_REST_Request $request Full details about the request.
* @return array $args.
**/
function wp_rest_filter_add_filter_param( $args, $request ) {
// Bail out if no filter parameter is set.
if ( empty( $request['filter'] ) || ! is_array( $request['filter'] ) ) {
return $args;
}
$filter = $request['filter'];
if ( isset( $filter['posts_per_page'] ) && ( (int) $filter['posts_per_page'] >= 1 && (int) $filter['posts_per_page'] <= 100 ) ) {
$args['posts_per_page'] = $filter['posts_per_page'];
}
global $wp;
$vars = apply_filters( 'rest_query_vars', $wp->public_query_vars );
function allow_meta_query( $valid_vars )
{
$valid_vars = array_merge( $valid_vars, array( 'meta_query', 'meta_key', 'meta_value', 'meta_compare' ) );
return $valid_vars;
}
$vars = allow_meta_query( $vars );
foreach ( $vars as $var ) {
if ( isset( $filter[ $var ] ) ) {
$args[ $var ] = $filter[ $var ];
}
}
return $args;
}
IMHO, a more better way would to include the additional function as a seperate plugin. So even when user changed theme, your api calls won't be affected.
Therefore I've developed a plugin for meta query in WordPress. Better yet, it supports ACF too!
WP REST Filter
After installation, just do a GET request in the following format.
https://domain.com/wp-json/acf/v3/customposttype?filter[meta_key]=acfkey&filter[meta_value]=acfvalue
Check out the plugin source code from Github.