0

Let's say, I have two categories, 'color' and 'temperature'. Each one has a number of sub-categories.

Wordpress does great job displaying posts in requested categories when I go to URL like this one:

http://www.example.com/category/pink+warm

Now I would like to do the same with json-rest-api plugin, via REST requests.

Is it possible? How to get posts in multiple categories (not in either, but in all of requested) using REST requests?

I've tried different URIs, but failed.

1

Assusming you are using V1 of the WP-API, did you tried the following endpoint:

http://www.example.com/wp-json/posts?filter[category_name]=pink+warm

I'm looking at the code in WP_JSON_Posts class and the get_post() should be able to handle that use case.

UPDATE: It seems there is a limitation with the way the query is built. Here is a potential workaround:

add_filter('json_query_var-category_name', function( $var ) {
   return str_replace(' ', '+', $filter[ $var ] );
});

Or even better (without new code)! Encode the + in the url with %2B:

http://www.example.com/wp-json/posts?filter[category_name]=pink%2Bwarm

This seems to work for me! See here for clarifications: https://bugs.php.net/bug.php?id=39078

Note: In V2 this endpoint will not work.

  • Yes, I use V1 of the WP-API. I've tried, and I get nothing. Let's say, querying pink only gives me 4 posts, querying warm gives 2, pink+warm gives nothing. However those 2 warm posts are definitely pink, I get them in the 1st query too. – brownian Jul 31 '15 at 13:39
  • 1
    However comma works as expected: pink,warm gives posts either pink or warm. – brownian Jul 31 '15 at 14:17
  • WordPress default parse_request method automatically replace space with a + sign which does not happen in the rest plugin. You can either you the filter on my updated comment or encode the + sign in the url yourself. – jonathanbardo Jul 31 '15 at 14:55
  • YES! --- I've already did the trick with str_replace and it works. I've did it in the code, so I am wrong here .) Anyway your answer is much better because you've proposed a plugin AND the trick with %2B which is acceptable too. THANKS. – brownian Jul 31 '15 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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