[wp-trac] [WordPress Trac] #29738: Support nested queries in WP_Tax_Query

WordPress Trac noreply at wordpress.org
Wed Sep 24 02:02:51 UTC 2014


#29738: Support nested queries in WP_Tax_Query
--------------------------+-----------------------------
 Reporter:  boonebgorges  |      Owner:
     Type:  enhancement   |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Taxonomy      |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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 of `tax_query` when filling in legacy query vars.
 Now that the tax_query can be nested, these hackish checks are no longer
 reliable. In `WP_Tax_Query`, during the sanitization of the query passed
 to the class, I generate a flat array of `queried_terms`, and then use
 that instead in `WP_Query`. IMO, the result is much cleaner code in
 `WP_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

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29738>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list