WordPress.org

Make WordPress Core

Opened 17 months ago

Last modified 5 weeks ago

#44326 accepted enhancement

Add relation parameter to REST API /wp/v2/posts endpoint

Reported by: earnjam Owned by: earnjam
Milestone: Future Release Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-unit-tests has-patch
Focuses: rest-api Cc:
PR Number:

Description

The REST API /wp/v2/posts endpoint allows filtering by terms across multiple taxonomies, but it always uses an AND relationship between them.

Basically:
/wp/v2/posts?tags=1&categories=2

Translates to:
All posts with Tag 1 AND Category 2

WP_Query supports passing a relation parameter when running a tax query across multiple taxonomies. It defaults to AND, which is what we do now, but it would be nice to support the ability to specify OR here as well.

Attachments (2)

44326.diff (5.8 KB) - added by earnjam 17 months ago.
44326.2.diff (5.8 KB) - added by earnjam 5 weeks ago.

Download all attachments as: .zip

Change History (10)

@earnjam
17 months ago

#1 @earnjam
17 months ago

44326.diff:

  • Adds a new parameter to the /wp/v2/posts endpoint called relation which accepts AND or OR. If left off, it defaults to the current functionality of AND.
  • Adds 2 new tests

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


4 months ago

#3 @TimothyBlynJacobs
4 months ago

  • Keywords needs-refresh added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release

This looks good to me!

I think we just need a different query parameter name that is a bit more specific to the term query. Perhaps terms_relation?

#4 @earnjam
4 months ago

Thanks! I chose relation since that was the name of the parameter in WP_Query, though I can see how that is pretty vague. I like term_relation, but I wonder if it should be something like tax_relation since it's the relationship between the taxonomies, not the individual terms.

This ticket actually spun out of #41287, which is for supporting the use an AND relationship between terms within a single taxonomy. I think the two would pair up nicely to extend what is possible through the API.

#5 @earnjam
4 months ago

  • Owner set to earnjam
  • Status changed from new to accepted

#6 @TimothyBlynJacobs
4 months ago

tax_relation sounds good to me!

@earnjam
5 weeks ago

#7 @earnjam
5 weeks ago

  • Keywords has-patch added; needs-refresh removed

44326.2.diff just updates the naming to tax_relation

This ticket was mentioned in Slack in #core-restapi by earnjam. View the logs.


5 weeks ago

Note: See TracTickets for help on using tickets.