7

I use Wp_query class to query my posts. 15 posts are in a template fetched on page load and their filtering works fine. Infinity scroll fetches for other posts using JSON restful services. The filter on the server side works fine but I don't know how to transfer the logic from the server to a proper query.

Meta_query array passed to Wp_query on the server:

meta_query

  0 =>  array (size=3)
    'key' => string 'homepage' (length=8)
    'value' => string 'false' (length=5)
    'compare' => string 'NOT EXISTS' (length=10)
  1 => array (size=2)
    'key' => string '_newsml_id' (length=10)
    'compare' => string 'NOT EXISTS' (length=10)
  'relation' => string 'AND' (length=3)

How to translate that into the query? something like:

/wp-json/posts?filter[meta_query][key]=homepage,_newsml_id&[meta_query][compare]=NOTEXISTS

or

/wp-json/posts?filter[meta_query][key]=homepage&filter[meta_query][key]=_newsml_id&[meta_query][compare]=NOTEXISTS

2 Answers 2

9

Sorry for answering my own question but it may help some other devs too.

I created this additional filter 'json_query_var-meta_query'that returns the necessary arguments.

function adjustQrry($data){
    $args = array();
    $args['relation'] = 'AND';

    foreach ($data as $key=>$value) {
        if ( 'relation' === $key ) {
            $args['relation'] = $data['relation'];
        }
        if (  substr($key, 0, 3) === 'key' ) {
            $arg_num = substr($key, 3);
            $args[(int)$arg_num]['key'] = $value;
        }
        if (  substr($key, 0, 7) === 'compare' ) {
            $arg_num_comp = substr($key, 7);
            $args[(int)$arg_num_comp]['compare'] = $value;
        }
    }
    return $args;
}

add_filter('json_query_var-meta_query', 'adjustQrry', 10, 1);

Now, I can call JSON restful like that to mimic the Wp_query posts filter already on the server:

?filter[meta_query][key]=_newsml_categories_newsstream&filter[meta_query][key2]=homepage&filter[meta_query][relation]=AND&filter[meta_query][compare]=NOT%20EXISTS&filter[meta_query][compare2]=NOT%20EXISTS

Ref: https://github.com/WP-API/WP-API/issues/337

2
  • 5
    No need to apologize for answering your own question, that's something you should do if you're able :)
    – Ian Dunn
    May 12, 2015 at 16:45
  • As @IanDunn points out, answering your own question is definitely a good thing. You can accept your own answer too, so that others know you found a solution
    – phatskat
    Sep 11, 2015 at 15:52
0

what about WP REST API V2? I think this is not working on v2!

add_filter('json_query_var-meta_query', 'adjustQrry', 10, 1);

should it be like this?

add_filter('rest_query_vars-meta_query', 'adjustQrry', 10, 1);
2
  • 3
    I think, you shouldn't create a new question as an answer, but as a new question. Anyway, I think this would work: github.com/WP-API/WP-API/issues/2452 Apr 20, 2016 at 11:33
  • @websupporter thank you. yeah that is my question too :) you helped much. thanks again.
    – Amino
    Apr 20, 2016 at 12:57

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.