WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#41908 closed enhancement (duplicate)

REST API gives wrong output when filtering posts with multiple category IDs - issue with AND operator

Reported by: subrataemfluence Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8.1
Component: REST API Keywords:
Focuses: rest-api Cc:
PR Number:

Description

Encountered an issue while working with REST API which I am trying to explain below with test cases:

Requirement:
Filter out posts which are associated with category ids 2, 4 AND 3.

REST API calls made:

http://example.com/wp-json/wp/v2/posts?filter[category__in][]=2&filter[category__in][]=3&filter[category__in][]=4

The above call fetched all available posts. Then I tried this:

http://example.com/wp-json/wp/v2/posts?categories=2&categories=4&categories=3

A better response this time, however, all posts having Category ID 3 (the last one in the list) are returned even some of them don't have category ID 2 or 3 and some of have another category ID along with 3. In short this call gave me posts like this:

Post A: 3, 9
Post B: 3
Post C: 3, 8, 12
Post D: 2, 3, 6
Post E: 2, 3, 4

When I am expecting only Post E to come up.

This time I changed the call a bit by putting the IDs in order:

http://example.com/wp-json/wp/v2/posts?categories=2&categories=3&categories=4

I got the correct result, i.e. only Post E came up this time.

To extend my test a bit further I edited the post (Post E), removed Category ID 2 from it and then tried the same call again. This time I did not expect any result to come up because I am searching for posts associated with Category IDs 2, 3 AND 4. But I still get Post E even it is not associated with Category ID 2 anymore.

Its a clean WP install, no plugin is there yet.

Change History (1)

#1 @dd32
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Type changed from defect (bug) to enhancement

Hi @subrataemfluence and welcome to Trac.

The WordPress REST API only currently supports OR taxonomy queries, there doesn't exist any AND queries.

There's an existing ticket tracking adding AND queries: #41287

Note: See TracTickets for help on using tickets.