[wp-trac] [WordPress Trac] #41287: Add AND query mode for taxonomy terms

WordPress Trac noreply at wordpress.org
Wed Jun 6 14:14:05 UTC 2018


#41287: Add AND query mode for taxonomy terms
-------------------------------------------------+-------------------------
 Reporter:  sebbb                                |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  REST API                             |     Version:  4.8
 Severity:  normal                               |  Resolution:
 Keywords:  dev-feedback has-patch has-unit-     |     Focuses:
  tests                                          |
-------------------------------------------------+-------------------------
Changes (by earnjam):

 * keywords:  needs-patch needs-unit-tests => dev-feedback has-patch has-
     unit-tests


Comment:

 In [attachment:41287.diff], I've added support for a new `{$tax}_and`
 parameter which queries using an AND relationship between terms. This
 follows the model of the existing parameter `{$tax}_exclude` for a NOT IN
 type query.

 I also considered something like `{$tax}_strict` for the parameter name,
 but went with `_and` because it more closely aligns with existing
 `WP_Query` arguments for `tag__and` and `category__and`

 Also, it may be beyond the scope of this ticket, in which case I can split
 it off into a new ticket, but I also added a new parameter, `relation`,
 which allows you to specify an OR relationship between multiple taxonomies
 being queried. This more closely mimics the functionality for a
 `tax_query` argument on `WP_Query`.

 So basically this is the current behavior:

 ||= URL =||= Tax Query Matches =||
 || `?tags=1,2` || `tag 1` **OR** `tag 2` ||
 || `?tags=1&categories=3` || `tag 1` **AND** `category 3` ||
 || `?tags=1,2&categories=3` || (`tag 1` **OR** `tag 2`) **AND** `category
 3` ||
 || `?tags=1,2&categories=3,4` || (`tag 1` **OR** `tag 2`) **AND**
 (`category 3` **OR** `category 4`) ||

 With this patch, you can use a `{$tax}_and` type parameter to force AND
 matching on the terms within a specific taxonomy.

 ||= URL =||= Tax Query Matches =||
 || `?tags_and=1,2` || `tag 1` **AND** `tag 2` ||
 || `?tags_and=1,2&categories=3` || `tag 1` **AND** `tag 2` **AND**
 `category 3` ||
 || `?tags_and=1,2&categories_and=3,4` || `tag 1` **AND** `tag 2` **AND**
 `category 3` **AND** `category 4` ||

 You can also use the `relation` parameter to allow you to make OR queries
 across multiple taxonomies.

 ||= URL =||= Tax Query Matches =||
 || `?tags=1&categories=3&relation=OR` || `tag 1` **OR** `category 3` ||
 || `?tags=1,2&categories=3&relation=OR` || `tag 1` **OR** `tag 2` **OR**
 `category 3` ||
 || `?tags=1,2&categories=3,4&relation=OR` || `tag 1` **OR** `tag 2` **OR**
 `category 3` **OR** `category 4` ||

 Or you can combine the two together to make very complex tax queries.

 ||= URL =||= Tax Query Matches =||
 || `?tags_and=1,2&tags=3&relation=OR` || (`tag 1` **AND** `tag 2`) **OR**
 `tag 3` ||
 || `?tags_and=1,2&categories=3&relation=OR` || (`tag 1` **AND** `tag 2`)
 **OR** `category 3`
 || `?tags_and=1,2&categories_and=3,4&relation=OR` || (`tag 1` **AND** `tag
 2`) **OR** (`category 3` **AND** `category 4`) ||
 || `?tags_and=1,2&categories_exclude=3,4&relation=OR` || (`tag 1` **AND**
 `tag 2`) **OR** ( **NOT** `category 3` **OR** `category4` ) ||

 You can even use the `relation` parameter for queries within a single
 taxonomy that use multiple types of operators:

 ||= URL =||= Tax Query Matches =||
 || `?tags_and=1,2&tags=3,4&relation=OR` || (`tag 1` **AND** `tag 2`)
 **OR** `tag 3` **OR** `tag 4` ||
 || `?tags_and=1,2&tags_exclude=2,3&relation=OR` || (`tag 1` **AND** `tag
 2`) **OR** ( **NOT** `tag 2` **OR** `tag 3` ) ||


 I added several additional tests, but with so many new query combination
 possibilities (it goes from 4 to 18 possible combinations when querying 2
 taxonomies), it might help to have a bit more coverage.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/41287#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list