#29738 closed enhancement (fixed)
Support nested queries in WP_Tax_Query
Reported by: | boonebgorges | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Taxonomy | Keywords: | has-patch |
Focuses: | Cc: |
Description
See #29642 for a related ticket for WP_Meta_Query.
The current syntax of WP_Tax_Query
is limited to a flat set of queries, linked by a single 'relation'. This limits the kinds of queries you can run. For example, the following type of query cannot be run with the current syntax:
SELECT ... WHERE ( 'post_tag' IN ( 'gum', 'cereal', 'candy' ) AND ( 'favorite_turtle' NOT IN ('Raphael') OR 'favorite_soda' = 'RC Cola' ) )
(Don't ask me what kind of WordPress installation would be querying for these things. A pretty cool one?)
I propose that WP_Tax_Query be restructured to allow for nested queries of arbitrary depth. The above translates to a tax_query
argument that looks like this:
'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'post_tag', 'terms' => array( 'gum', 'cereal', 'candy' ), 'field' => 'slug', 'operator' => 'IN', ) array( 'relation' => 'OR', array( 'taxonomy' => 'favorite_turtle', 'terms' => array( 'Raphael' ), 'field' => 'name', 'operator' => 'NOT IN', ), array( 'taxonomy' => 'favorite_soda', 'terms' => array( 'RC' ), 'field' => 'name', ), ), ),
The attached patch contains this restructuring. Notes:
- Complete backward compatibility. Query syntax does not change, nor does the SQL generated by the class. (With the exception of whitespace: I added indentation to make the nesting clearer.)
- Unit tests. There are a few nesting-specific unit tests included. I've put them in a separate file (tests/term/query-nested.php) so that they'd apply cleanly. The patch also passes my extended unit tests, described here: #29718
- Some modifications to
WP_Query
are included.WP_Query
does a couple of pretty hackish checks oftax_query
when filling in legacy query vars. Now that the tax_query can be nested, these hackish checks are no longer reliable. InWP_Tax_Query
, during the sanitization of the query passed to the class, I generate a flat array ofqueried_terms
, and then use that instead inWP_Query
. IMO, the result is much cleaner code inWP_Query
, and it also fixes a couple of odd bugs in the previous backward compatibility code. See https://core.trac.wordpress.org/ticket/29718#comment:3 for more details. - Aside from the recursion stuff, the internal SQL generation logic of
WP_Tax_Query
hasn't been touched. - Did I mention that all the unit tests pass? #29718
Attachments (2)
Change History (12)
This ticket was mentioned in IRC in #wordpress-dev by boonebgorges. View the logs.
10 years ago
This ticket was mentioned in IRC in #wordpress-dev by boonebgorges. View the logs.
10 years ago
#3
@
10 years ago
- Milestone changed from Awaiting Review to 4.1
- Owner set to boonebgorges
- Status changed from new to assigned
#5
@
10 years ago
The docs in 29738.2.patch look fabutastic.
29738.2.patch is a refresh that does the following:
WP_Meta_Query
[29887]:sanitize_query()
mirrors more closely in terms of internal logic and variable namingget_sql_clauses()
andget_sql_for_query()
are now duplicates of their brethren inWP_Meta_Query