• Hi there!

    I am trying to request posts via the WP Rest API that have multiple specific categories applied.

    Using WP_Query, I’m able to do category__and with an array of IDs, but I can’t figure out how to achieve a similar result via the API.

    I have looked at the rest-filter plugin, but that seems to also do an “OR” rather than “AND”.

    For instance:
    Post A: Categories 1,2
    Post B: Category 1

    Request: categories=1,2
    Expected response: Post A

    Thanks!

    • This topic was modified 8 years, 9 months ago by averydev.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Try this:
    http://example.com/wp-json/wp/v2/posts?categories=5&categories=6

    Above will filter out records which are associated with category ID 5 AND 6.

    There is an issue however (I noticed) while building the query string. If you are using the same parameter (like categories here), the IDs you want to consider in the actual query has to be placed in ascending order, meaning

    http://example.com/wp-json/wp/v2/posts?categories=5&categories=6 will give you the desired output but

    http://example.com/wp-json/wp/v2/posts?categories=6&categories=5, will not! It will (as far I have seen) will only consider the last one in the list, i.e. all posts having category ID 5 will be fetched and ID 6 will simply be ignored!!

    Btw, you can use same parameter even more than twice like

    http://example.com/wp-json/wp/v2/posts?categories=5&categories=6&categories=12, but only thing to remember is the order of IDs as I mentioned above.

    Let me know if this helps!

    Moderator bcworkz

    (@bcworkz)

    Very interesting Subrata! I only get the last category no matter which order previous ones are in. What does work for me is the same thing we need to do in form multiple selects, append [] to the name so that PHP collects the data into an array, like so:
    example.com/wp-json/wp/v2/posts?categories[]=5&categories[]=6&categories[]=12,

    In the case of REST, it was never intended that you can always get everything you need in a single request. There generally shouldn’t be any reason your RESTful app couldn’t make multiple requests and combine the results.

    • This reply was modified 8 years, 9 months ago by bcworkz.
    Thread Starter averydev

    (@averydev)

    Hey there,

    The approach of the ascending order categories doesn’t seem to have any effect, my guess is that it’s a coincidence that the sequence has an effect for you.

    categories[]=5&categories[]=6&categories[]=12 similarly has the effect of an “OR” rather than an “AND” in my experience.

    The reason that multiple requests won’t work is this — Imagine if you had 3 categories, each with 1000 posts, but only 10 posts that overlapped all 3. You wouldn’t be making 3 requests — you’d have to load all of the posts from one of the categories, and then in JS check each item to see if the categories matched.

    That’d be 100x the data transfer compared to what was actually needed. It seems to me that this is something that would best be handled at the db, since it is touching all of the data.

    • This reply was modified 8 years, 9 months ago by averydev.
    Moderator bcworkz

    (@bcworkz)

    Yes, whether you want AND logic or OR makes a huge difference. Multiple requests for OR is not a big deal, but as you point out, AND may not be very efficient. In situations where simple GET requests become problematic, consider using POST requests where passing the needed parameters is not as cumbersome and limited. For unique situations that the API doesn’t support at all, or where achieving your goal is very cumbersome, you can create custom endpoints that address you unique requirements. If something is possible with PHP at all, there’s usually a way to get there in WP, though it may not be easy.

    Thread Starter averydev

    (@averydev)

    Hi there bcworkz.

    I’m indeed only looking for AND right now. I don’t think that GET vs POST will make any difference since the same parameters are available regardless of which method, unless I’m missing some documentation somewhere.

    There is an upper limit to the number of characters in a get request but that’s not an issue here.

    It sounds like I’m going to need to build a custom endpoint to do this. I’m surprised that there isn’t a way to do this request since it’s such a basic one…

    Hello @averydev, according to your version:

    “The approach of the ascending order categories doesn’t seem to have any effect, my guess is that it’s a coincidence that the sequence has an effect for you.”

    I checked with different ID combinations and orders and they all worked the way I mentioned. Can you please share an example which does not hold true?

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘WordPress Rest API equivalent to category__and’ is closed to new replies.