12

I have been meddling with REST API and I am stuck with this: How can I filter posts in both cat1 and cat2?

For now, ?categories[]=45&categories[]=50 returns in category ID 45 OR 50 - how can I get posts in 45 AND 50?

Thanks in advance.

  • Have you found a solution for this? – Corey Feb 3 '17 at 17:03
  • @Corey: Nopes. Guess it's not possible,as of now (saw some other plugin support posts claiming that) I was using API for an app with Ionic, I loaded them separately and intersected instead. Leaving it open, in case my learning is incomplete. Did read somewhere where Plugin is edited (not at all recommended) to achieve it. – Jesse P Francis Feb 5 '17 at 15:17
6

Multiple categories can be separated by comma like below

http://example.com/wp-json/wp/v2/posts?categories=20,30

hope this helps

| improve this answer | |
  • Request is for the AND search, this will give you OR. – Tom Woodward Sep 8 '19 at 23:29
6

@Jesse see: WP-API/WP-API#2990

Since WP 4.7, filter has been removed from WP-API.

You need to use this plugin: https://github.com/WP-API/rest-filter

| improve this answer | |
1

You should be able to access multiple categories by using the following:

http://YOURSITE.DEV/wp-json/wp/v2/posts?categories=45+50

Hope that helps!

Cheers

| improve this answer | |
1

Install the filter plugin Austin mentioned (https://github.com/WP-API/rest-filter) and try ?filter[categories]=cat_one_slug%2Bcat_two_slug.

I found out that %2B is the code equivalent of the + symbol.

Normally we would use + for the AND operator but unfortunately it gets converted into a space so use %2B instead.

| improve this answer | |
1

There doesn't seem to be a way to do this in the current version of the API. Without using a plugin, it can be achieved with a custom endpoint or by using the rest query filter function for the specific post type.

Here's a rough filter function that adds a parameter called cat_relation:

add_filter( 'rest_post_query', function( $args, $request ) {
    if($request['cat_relation'] == 'AND') {
        $args['category__and'] = $request['categories'];
    }
    return $args;
}, 10, 2);

So an example request URL would be:
http://example.com/wp-json/wp/v2/posts?categories=17,8&cat_relation=AND

| improve this answer | |
0

This is what I did, It works fine.

http://example.com/wp-json/wp/v2/articles/?_embed&categories=1,2,3,4&per_page=30
| improve this answer | |
0

Filtering posts based on multiple custom taxonomy terms based on logical "AND", "OR" condition.

Here is what you need to do in the newer versions of Wordpress (v5 and above)

Lets say you have two custom taxonomies: sector and offerings.

Getting all posts that are tagged with taxonomy ID 51 "OR" 52 beloning to the taxonmy term "sector" "OR" Getting all posts that are tagged with taxonomy ID 57 "AND" 58 beloning to the taxonomy term "offering"

https://mywordpressbackend.com/wp-json/wp/v2/posts?sector=51+52&offering=57,58&tax_relation=OR

Getting all posts that are tagged with taxonomy ID 51 "OR" 52 beloning to the taxonmy term "sector" "AND" Getting all posts that are tagged with taxonomy ID 57 "AND" 58 beloning to the taxonomy term "offering"

https://mywordpressbackend.com/wp-json/wp/v2/posts?sector=51+52&offering=57,58&tax_relation=AND
| improve this answer | |
-1

For those who are using Rest API v3, It works for me:

http://example.com/wp-json/wc/v3/products/?category=42,43
| improve this answer | |
  • This is the WooCommerce REST API for products and product categories. The question was about posts and post categories with the WordPress REST API. – Jacob Peattie Mar 25 at 6:53

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.