WordPress.org

Make WordPress Core

Opened 8 months ago

Closed 8 months ago

#52468 closed defect (bug) (duplicate)

Filtering posts with multiple tags and tax relation AND returning unexpected results

Reported by: milana_cap Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: REST API Keywords: reporter-feedback
Focuses: rest-api Cc:

Description

I've been trying to get posts from two tags (tax relation AND) with following arguments:

  • /wp-json/wp/v2/posts?tags=ID,ID&tax_relation=AND
  • /wp-json/wp/v2/posts?tags=ID+ID&tax_relation=AND
  • /wp-json/wp/v2/posts?tags[]=ID&tags[]=ID

None of these worked. They either returned on the first tag or all published posts.

Then I found this solution (by trial and error method):

  • /wp-json/wp/v2/posts?tags=ID&&ID

This one seems to work but only if the first ID is lower number.

I've tested this on several different WordPress installs (5.6 and 5.6.1) and all had the same results. You can quickly see it here:

This one doesn't work: https://make.wordpress.org/core/wp-json/wp/v2/posts?tags=3038&&1443

But this one works: https://make.wordpress.org/core/wp-json/wp/v2/posts?tags=1443&&3038

I haven't tested this with other taxonomies.

Change History (2)

#1 @TimothyBlynJacobs
8 months ago

  • Keywords reporter-feedback added

Thanks for the ticket @milana_cap!

I believe this is expected behavior. tax_relation is for each tax_query. A separate tax_query is built for each taxonomy query parameter. So tags, tags_exclude, categories, categories_exclude etc... would each be a separate tax_query. You could then use tax_relation to say that the posts must have any of those tags, and any of those categories, etc...

I think what you are looking for is #41287.

I'm not sure why ?tags=ID&&ID works, but I wouldn't consider it an official API.

#2 @TimothyBlynJacobs
8 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #41287.

Closing per my previous comment.

Note: See TracTickets for help on using tickets.