10

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?

0

2 Answers 2

5

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');
2
0

I know this question was solved, but this plugin is out and solved my problem https://wordpress.org/plugins/wp-rest-filter/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.