<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[29891] trunk: Introduce support for nested queries in WP_Tax_Query.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/29891">29891</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/29891","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-10-14 04:02:41 +0000 (Tue, 14 Oct 2014)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Introduce support for nested queries in WP_Tax_Query.

Previously, tax query arguments could be joined by a single AND or OR relation.
Now, these queries can be arbitrarily nested, allowing clauses to be linked
together with multiple relations.

In a few places, WP_Query runs through a list of clauses in a tax_query in order
to set certain query vars for backward compatibility. The necessary changes have
been made to WP_Query to support this feature with the new complex structure of
tax_query. Unit tests are included for these backward compatibility fixes.

Unit tests for the new nesting syntax are included.

Props boonebgorges.
Fixes <a href="https://core.trac.wordpress.org/ticket/29718">#29718</a>. See <a href="https://core.trac.wordpress.org/ticket/29738">#29738</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesqueryphp">trunk/src/wp-includes/query.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunittestspostqueryphp">trunk/tests/phpunit/tests/post/query.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/query.php   2014-10-14 02:16:28 UTC (rev 29890)
+++ trunk/src/wp-includes/query.php     2014-10-14 04:02:41 UTC (rev 29891)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1672,7 +1672,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->parse_tax_query( $qv );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $this->tax_query->queries as $tax_query ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( 'NOT IN' != $tax_query['operator'] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( ! is_array( $tax_query ) ) {
+                                       continue;
+                               }
+
+                               if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         switch ( $tax_query['taxonomy'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                case 'category':
</span><span class="cx" style="display: block; padding: 0 10px">                                                        $this->is_category = true;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2687,7 +2691,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( empty($post_type) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                // Do a fully inclusive search for currently registered post types of queried taxonomies
</span><span class="cx" style="display: block; padding: 0 10px">                                $post_type = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $taxonomies = array_keys( $this->tax_query->queried_terms );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( array_intersect( $taxonomies, $object_taxonomies ) )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2704,51 +2708,56 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Back-compat
-               if ( !empty($this->tax_query->queries) ) {
-                       $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
-                       if ( !empty( $tax_query_in_and ) ) {
-                               if ( !isset( $q['taxonomy'] ) ) {
-                                       foreach ( $tax_query_in_and as $a_tax_query ) {
-                                               if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
-                                                       $q['taxonomy'] = $a_tax_query['taxonomy'];
-                                                       if ( 'slug' == $a_tax_query['field'] )
-                                                               $q['term'] = $a_tax_query['terms'][0];
-                                                       else
-                                                               $q['term_id'] = $a_tax_query['terms'][0];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /*
+                * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
+                * 'category_name' vars are set for backward compatibility.
+                */
+               if ( ! empty( $this->tax_query->queried_terms ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                        break;
-                                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Set 'taxonomy', 'term', and 'term_id' to the
+                        * first taxonomy other than 'post_tag' or 'category'.
+                        */
+                       if ( ! isset( $q['taxonomy'] ) ) {
+                               foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
+                                       if ( empty( $queried_items['terms'][0] ) ) {
+                                               continue;
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
-                               if ( ! empty( $cat_query ) ) {
-                                       $cat_query = reset( $cat_query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
+                                               $q['taxonomy'] = $queried_taxonomy;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        if ( ! empty( $cat_query['terms'][0] ) ) {
-                                               $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
-                                               if ( $the_cat ) {
-                                                       $this->set( 'cat', $the_cat->term_id );
-                                                       $this->set( 'category_name', $the_cat->slug );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         if ( 'slug' === $queried_items['field'] ) {
+                                                       $q['term'] = $queried_items['terms'][0];
+                                               } else {
+                                                       $q['term_id'] = $queried_items['terms'][0];
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                unset( $the_cat );
</del><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                unset( $cat_query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
-                               if ( ! empty( $tag_query ) ) {
-                                       $tag_query = reset( $tag_query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // 'cat', 'category_name', 'tag_id'
+                       foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
+                               if ( empty( $queried_items['terms'][0] ) ) {
+                                       continue;
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        if ( ! empty( $tag_query['terms'][0] ) ) {
-                                               $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
-                                               if ( $the_tag )
-                                                       $this->set( 'tag_id', $the_tag->term_id );
-                                               unset( $the_tag );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( 'category' === $queried_taxonomy ) {
+                                       $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' );
+                                       if ( $the_cat ) {
+                                               $this->set( 'cat', $the_cat->term_id );
+                                               $this->set( 'category_name', $the_cat->slug );
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        unset( $the_cat );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                unset( $tag_query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               if ( 'post_tag' === $queried_taxonomy ) {
+                                       $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' );
+                                       if ( $the_tag ) {
+                                               $this->set( 'tag_id', $the_tag->term_id );
+                                       }
+                                       unset( $the_tag );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php        2014-10-14 02:16:28 UTC (rev 29890)
+++ trunk/src/wp-includes/taxonomy.php  2014-10-14 04:02:41 UTC (rev 29891)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -627,25 +627,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Container class for a multiple taxonomy query.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Class for generating SQL clauses that filter a primary query according to object taxonomy terms.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * `WP_Tax_Query` is a helper that allows primary query classes, such as {@see WP_Query}, to filter
+ * their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
+ * to the primary SQL query string.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * @since 3.1.0
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Tax_Query {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * List of taxonomy queries. A single taxonomy query is an associative array:
-        * - 'taxonomy' string The taxonomy being queried. Optional when using the term_taxonomy_id field.
-        * - 'terms' string|array The list of terms
-        * - 'field' string (optional) Which term field is being used.
-        *              Possible values: 'term_id', 'slug', 'name', or 'term_taxonomy_id'
-        *              Default: 'term_id'
-        * - 'operator' string (optional)
-        *              Possible values: 'AND', 'IN' or 'NOT IN'.
-        *              Default: 'IN'
-        * - 'include_children' bool (optional) Whether to include child terms. Requires that a taxonomy be specified.
-        *              Default: true
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Array of taxonomy queries.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * See {@see WP_Tax_Query::__construct()} for information on tax query arguments.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @since 3.1.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         * @var array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -668,30 +664,53 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @access private
</span><span class="cx" style="display: block; padding: 0 10px">         * @var string
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private static $no_results = array( 'join' => '', 'where' => ' AND 0 = 1' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static $no_results = array( 'join' => array( '' ), 'where' => array( '0 = 1' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * A flat list of table aliases used in the JOIN clauses.
+        *
+        * @since 4.1.0
+        * @access protected
+        * @var array
+        */
+       protected $table_aliases = array();
+
+       /**
+        * Terms and taxonomies fetched by this query.
+        *
+        * We store this data in a flat array because they are referenced in a
+        * number of places by WP_Query.
+        *
+        * @since 4.1.0
+        * @access public
+        * @var array
+        */
+       public $queried_terms = array();
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Constructor.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Parses a compact tax query and sets defaults.
-        *
</del><span class="cx" style="display: block; padding: 0 10px">          * @since 3.1.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array $tax_query A compact tax query:
-        *  array(
-        *    'relation' => 'OR',
-        *    array(
-        *      'taxonomy' => 'tax1',
-        *      'terms' => array( 'term1', 'term2' ),
-        *      'field' => 'slug',
-        *    ),
-        *    array(
-        *      'taxonomy' => 'tax2',
-        *      'terms' => array( 'term-a', 'term-b' ),
-        *      'field' => 'slug',
-        *    ),
-        *  )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array $tax_query {
+        *     Array of taxonoy query clauses.
+        *
+        *     @type string $relation Optional. The MySQL keyword used to join
+        *                            the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
+        *     @type array {
+        *         Optional. An array of first-order clause parameters, or another fully-formed tax query.
+        *
+        *         @type string           $taxonomy         Taxonomy being queried. Optional when field=term_taxonomy_id.
+        *         @type string|int|array $terms            Term or terms to filter by.
+        *         @type string           $field            Field to match $terms against. Accepts 'term_id', 'slug',
+        *                                                 'name', or 'term_taxonomy_id'. Default: 'term_id'.
+        *         @type string           $operator         MySQL operator to be used with $terms in the WHERE clause.
+        *                                                  Accepts 'AND', 'IN', or 'OR. Default: 'IN'.
+        *         @type bool             $include_children Optional. Whether to include child terms.
+        *                                                  Requires a $taxonomy. Default: true.
+        *     }
+        * }
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct( $tax_query ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -700,125 +719,328 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->relation = 'AND';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->queries = $this->sanitize_query( $tax_query );
+       }
+
+       /**
+        * Ensure the `tax_query` argument passed to the class constructor is well-formed.
+        *
+        * Ensures that each query-level clause has a 'relation' key, and that
+        * each first-order clause contains all the necessary keys from $defaults.
+        *
+        * @since 4.1.0
+        * @access public
+        *
+        * @param  array $queries Array of queries clauses.
+        * @return array Sanitized array of query clauses.
+        */
+       public function sanitize_query( $queries ) {
+               $cleaned_query = array();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $defaults = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'taxonomy' => '',
</span><span class="cx" style="display: block; padding: 0 10px">                        'terms' => array(),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'include_children' => true,
</del><span class="cx" style="display: block; padding: 0 10px">                         'field' => 'term_id',
</span><span class="cx" style="display: block; padding: 0 10px">                        'operator' => 'IN',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'include_children' => true,
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $tax_query as $query ) {
-                       if ( ! is_array( $query ) )
-                               continue;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $queries as $key => $query ) {
+                       if ( 'relation' === $key ) {
+                               $cleaned_query['relation'] = $query;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $query = array_merge( $defaults, $query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // First-order clause.
+                       } else if ( self::is_first_order_clause( $query ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $query['terms'] = (array) $query['terms'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $cleaned_clause = array_merge( $defaults, $query );
+                               $cleaned_clause['terms'] = (array) $cleaned_clause['terms'];
+                               $cleaned_query[] = $cleaned_clause;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->queries[] = $query;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         /*
+                                * Keep a copy of the clause in the flate
+                                * $queried_terms array, for use in WP_Query.
+                                */
+                               if ( ! empty( $cleaned_clause['taxonomy'] ) && 'NOT IN' !== $cleaned_clause['operator'] ) {
+                                       $taxonomy = $cleaned_clause['taxonomy'];
+                                       if ( ! isset( $this->queried_terms[ $taxonomy ] ) ) {
+                                               $this->queried_terms[ $taxonomy ] = array();
+                                       }
+
+                                       /*
+                                        * Backward compatibility: Only store the first
+                                        * 'terms' and 'field' found for a given taxonomy.
+                                        */
+                                       if ( ! empty( $cleaned_clause['terms'] ) && ! isset( $this->queried_terms[ $taxonomy ]['terms'] ) ) {
+                                               $this->queried_terms[ $taxonomy ]['terms'] = $cleaned_clause['terms'];
+                                       }
+
+                                       if ( ! empty( $cleaned_clause['field'] ) && ! isset( $this->queried_terms[ $taxonomy ]['field'] ) ) {
+                                               $this->queried_terms[ $taxonomy ]['field'] = $cleaned_clause['field'];
+                                       }
+                               }
+
+                       // Otherwise, it's a nested query, so we recurse.
+                       } else if ( is_array( $query ) ) {
+                               $cleaned_subquery = $this->sanitize_query( $query );
+
+                               if ( ! empty( $cleaned_subquery ) ) {
+                                       $cleaned_query[] = $cleaned_subquery;
+                               }
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               return $cleaned_query;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Determine whether a clause is first-order.
+        *
+        * A "first-order" clause is one that contains any of the first-order
+        * clause keys ('terms', 'taxonomy', 'include_children', 'field',
+        * 'operator'). An empty clause also counts as a first-order clause,
+        * for backward compatibility. Any clause that doesn't meet this is
+        * determined, by process of elimination, to be a higher-order query.
+        *
+        * @since 4.1.0
+        * @access protected
+        *
+        * @param  array $query Tax query arguments.
+        * @return bool  Whether the query clause is a first-order clause.
+        */
+       protected static function is_first_order_clause( $query ) {
+               return empty( $query ) || array_key_exists( 'terms', $query ) || array_key_exists( 'taxonomy', $query ) || array_key_exists( 'include_children', $query ) || array_key_exists( 'field', $query ) || array_key_exists( 'operator', $query );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Generates SQL clauses to be appended to a main query.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.1.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $primary_table
-        * @param string $primary_id_column
-        * @return array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $primary_table     Database table where the object being filtered is stored (eg wp_users).
+        * @param string $primary_id_column ID column for the filtered object in $primary_table.
+        * @return array {
+        *     Array containing JOIN and WHERE SQL clauses to append to the main query.
+        *
+        *     @type string $join  SQL fragment to append to the main JOIN clause.
+        *     @type string $where SQL fragment to append to the main WHERE clause.
+        * }
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_sql( $primary_table, $primary_id_column ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                global $wpdb;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->primary_table = $primary_table;
+               $this->primary_id_column = $primary_id_column;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $join = '';
-               $where = array();
-               $i = 0;
-               $count = count( $this->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $this->get_sql_clauses();
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $this->queries as $index => $query ) {
-                       $this->clean_query( $query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Generate SQL clauses to be appended to a main query.
+        *
+        * Called by the public {@see WP_Tax_Query::get_sql()}, this method
+        * is abstracted out to maintain parity with the other Query classes.
+        *
+        * @since 4.1.0
+        * @access protected
+        *
+        * @return array {
+        *     Array containing JOIN and WHERE SQL clauses to append to the main query.
+        *
+        *     @type string $join  SQL fragment to append to the main JOIN clause.
+        *     @type string $where SQL fragment to append to the main WHERE clause.
+        * }
+        */
+       protected function get_sql_clauses() {
+               $sql = $this->get_sql_for_query( $this->queries );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( is_wp_error( $query ) ) {
-                               return self::$no_results;
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $sql['where'] ) ) {
+                       $sql['where'] = ' AND ' . $sql['where'];
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $terms = $query['terms'];
-                       $operator = strtoupper( $query['operator'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $sql;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( 'IN' == $operator ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Generate SQL clauses for a single query array.
+        *
+        * If nested subqueries are found, this method recurses the tree to
+        * produce the properly nested SQL.
+        *
+        * @since 4.1.0
+        * @access protected
+        *
+        * @param array $query Query to parse.
+        * @param int   $depth Optional. Number of tree levels deep we currently are.
+        *              Used to calculate indentation.
+        * @return array {
+        *     Array containing JOIN and WHERE SQL clauses to append to a single query array.
+        *
+        *     @type string $join  SQL fragment to append to the main JOIN clause.
+        *     @type string $where SQL fragment to append to the main WHERE clause.
+        * }
+        */
+       protected function get_sql_for_query( $query, $depth = 0 ) {
+               $sql_chunks = array(
+                       'join'  => array(),
+                       'where' => array(),
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( empty( $terms ) ) {
-                                       if ( 'OR' == $this->relation ) {
-                                               if ( ( $index + 1 === $count ) && empty( $where ) ) {
-                                                       return self::$no_results;
-                                               }
-                                               continue;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $sql = array(
+                       'join'  => '',
+                       'where' => '',
+               );
+
+               $indent = '';
+               for ( $i = 0; $i < $depth; $i++ ) {
+                       $indent .= "  ";
+               }
+
+               foreach ( $query as $key => $clause ) {
+                       if ( 'relation' === $key ) {
+                               $relation = $query['relation'];
+                       } else if ( is_array( $clause ) ) {
+
+                               // This is a first-order clause.
+                               if ( $this->is_first_order_clause( $clause ) ) {
+                                       $clause_sql = $this->get_sql_for_clause( $clause, $query );
+
+                                       $where_count = count( $clause_sql['where'] );
+                                       if ( ! $where_count ) {
+                                               $sql_chunks['where'][] = '';
+                                       } else if ( 1 === $where_count ) {
+                                               $sql_chunks['where'][] = $clause_sql['where'][0];
</ins><span class="cx" style="display: block; padding: 0 10px">                                         } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                return self::$no_results;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
+                               // This is a subquery, so we recurse.
+                               } else {
+                                       $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
+
+                                       $sql_chunks['where'][] = $clause_sql['where'];
+                                       $sql_chunks['join'][]  = $clause_sql['join'];
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        }
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $terms = implode( ',', $terms );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Filter to remove empties.
+               $sql_chunks['join']  = array_filter( $sql_chunks['join'] );
+               $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $alias = $i ? 'tt' . $i : $wpdb->term_relationships;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $relation ) ) {
+                       $relation = 'AND';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $join .= " INNER JOIN $wpdb->term_relationships";
-                               $join .= $i ? " AS $alias" : '';
-                               $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Filter duplicate JOIN clauses and combine into a single string.
+               if ( ! empty( $sql_chunks['join'] ) ) {
+                       $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $where[] = "$alias.term_taxonomy_id $operator ($terms)";
-                       } elseif ( 'NOT IN' == $operator ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Generate a single WHERE clause with proper brackets and indentation.
+               if ( ! empty( $sql_chunks['where'] ) ) {
+                       $sql['where'] = '( ' . "\n  " . $indent . implode( ' ' . "\n  " . $indent . $relation . ' ' . "\n  " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( empty( $terms ) ) {
-                                       continue;
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $sql;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $terms = implode( ',', $terms );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Generate SQL JOIN and WHERE clauses for a first-order query clause.
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $where[] = "$primary_table.$primary_id_column NOT IN (
-                                       SELECT object_id
-                                       FROM $wpdb->term_relationships
-                                       WHERE term_taxonomy_id IN ($terms)
-                               )";
-                       } elseif ( 'AND' == $operator ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 4.1.0
+        * @access public
+        *
+        * @param  array $clause       Query clause.
+        * @param  array $parent_query Parent query array.
+        * @return array {
+        *     Array containing JOIN and WHERE SQL clauses to append to a first-order query.
+        *
+        *     @type string $join  SQL fragment to append to the main JOIN clause.
+        *     @type string $where SQL fragment to append to the main WHERE clause.
+        * }
+        */
+       public function get_sql_for_clause( $clause, $parent_query ) {
+               global $wpdb;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( empty( $terms ) ) {
-                                       continue;
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $sql = array(
+                       'where' => array(),
+                       'join'  => array(),
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $num_terms = count( $terms );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $join = '';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $terms = implode( ',', $terms );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->clean_query( $clause );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $where[] = "(
-                                       SELECT COUNT(1)
-                                       FROM $wpdb->term_relationships
-                                       WHERE term_taxonomy_id IN ($terms)
-                                       AND object_id = $primary_table.$primary_id_column
-                               ) = $num_terms";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( is_wp_error( $clause ) ) {
+                       return self::$no_results;
+               }
+
+               $terms = $clause['terms'];
+               $operator = strtoupper( $clause['operator'] );
+
+               if ( 'IN' == $operator ) {
+
+                       if ( empty( $terms ) ) {
+                               return self::$no_results;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $i++;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $terms = implode( ',', $terms );
+
+                       $i = count( $this->table_aliases );
+                       $alias = $i ? 'tt' . $i : $wpdb->term_relationships;
+                       $this->table_aliases[] = $alias;
+
+                       $join .= " INNER JOIN $wpdb->term_relationships";
+                       $join .= $i ? " AS $alias" : '';
+                       $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
+
+                       $where = "$alias.term_taxonomy_id $operator ($terms)";
+
+               } elseif ( 'NOT IN' == $operator ) {
+
+                       if ( empty( $terms ) ) {
+                               continue;
+                       }
+
+                       $terms = implode( ',', $terms );
+
+                       $where = "$this->primary_table.$this->primary_id_column NOT IN (
+                               SELECT object_id
+                               FROM $wpdb->term_relationships
+                               WHERE term_taxonomy_id IN ($terms)
+                       )";
+
+               } elseif ( 'AND' == $operator ) {
+
+                       if ( empty( $terms ) ) {
+                               continue;
+                       }
+
+                       $num_terms = count( $terms );
+
+                       $terms = implode( ',', $terms );
+
+                       $where = "(
+                               SELECT COUNT(1)
+                               FROM $wpdb->term_relationships
+                               WHERE term_taxonomy_id IN ($terms)
+                               AND object_id = $this->primary_table.$this->primary_id_column
+                       ) = $num_terms";
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $where ) ) {
-                       $where = ' AND ( ' . implode( " $this->relation ", $where ) . ' )';
-               } else {
-                       $where = '';
-               }
-               return compact( 'join', 'where' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $sql['join'][]  = $join;
+               $sql['where'][] = $where;
+               return $sql;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Validates a single query.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.2.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @access private
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array &$query The single query
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array &$query The single query.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        private function clean_query( &$query ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( empty( $query['taxonomy'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -858,8 +1080,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.2.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array &$query The single query
-        * @param string $resulting_field The resulting field
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array  &$query          The single query.
+        * @param string $resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',
+        *                                or 'term_id'. Default: 'term_id'.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function transform_query( &$query, $resulting_field ) {
</span><span class="cx" style="display: block; padding: 0 10px">                global $wpdb;
</span></span></pre></div>
<a id="trunktestsphpunittestspostqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/query.php  2014-10-14 02:16:28 UTC (rev 29890)
+++ trunk/tests/phpunit/tests/post/query.php    2014-10-14 04:02:41 UTC (rev 29891)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1542,6 +1542,218 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 29738
+        * @group taxonomy
+        */
+       public function test_tax_query_two_nested_queries() {
+               register_taxonomy( 'foo', 'post' );
+               register_taxonomy( 'bar', 'post' );
+
+               $foo_term_1 = $this->factory->term->create( array(
+                       'taxonomy' => 'foo',
+               ) );
+               $foo_term_2 = $this->factory->term->create( array(
+                       'taxonomy' => 'foo',
+               ) );
+               $bar_term_1 = $this->factory->term->create( array(
+                       'taxonomy' => 'bar',
+               ) );
+               $bar_term_2 = $this->factory->term->create( array(
+                       'taxonomy' => 'bar',
+               ) );
+
+               $p1 = $this->factory->post->create();
+               $p2 = $this->factory->post->create();
+               $p3 = $this->factory->post->create();
+
+               wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
+               wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
+               wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
+               wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
+               wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
+               wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
+
+               $q = new WP_Query( array(
+                       'fields' => 'ids',
+                       'update_post_meta_cache' => false,
+                       'update_post_term_cache' => false,
+                       'tax_query' => array(
+                               'relation' => 'OR',
+                               array(
+                                       'relation' => 'AND',
+                                       array(
+                                               'taxonomy' => 'foo',
+                                               'terms' => array( $foo_term_1 ),
+                                               'field' => 'term_id',
+                                       ),
+                                       array(
+                                               'taxonomy' => 'bar',
+                                               'terms' => array( $bar_term_1 ),
+                                               'field' => 'term_id',
+                                       ),
+                               ),
+                               array(
+                                       'relation' => 'AND',
+                                       array(
+                                               'taxonomy' => 'foo',
+                                               'terms' => array( $foo_term_2 ),
+                                               'field' => 'term_id',
+                                       ),
+                                       array(
+                                               'taxonomy' => 'bar',
+                                               'terms' => array( $bar_term_2 ),
+                                               'field' => 'term_id',
+                                       ),
+                               ),
+                       ),
+               ) );
+
+               _unregister_taxonomy( 'foo' );
+               _unregister_taxonomy( 'bar' );
+
+               $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
+       }
+
+       /**
+        * @ticket 29738
+        * @group taxonomy
+        */
+       public function test_tax_query_one_nested_query_one_first_order_query() {
+               register_taxonomy( 'foo', 'post' );
+               register_taxonomy( 'bar', 'post' );
+
+               $foo_term_1 = $this->factory->term->create( array(
+                       'taxonomy' => 'foo',
+               ) );
+               $foo_term_2 = $this->factory->term->create( array(
+                       'taxonomy' => 'foo',
+               ) );
+               $bar_term_1 = $this->factory->term->create( array(
+                       'taxonomy' => 'bar',
+               ) );
+               $bar_term_2 = $this->factory->term->create( array(
+                       'taxonomy' => 'bar',
+               ) );
+
+               $p1 = $this->factory->post->create();
+               $p2 = $this->factory->post->create();
+               $p3 = $this->factory->post->create();
+
+               wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
+               wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
+               wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
+               wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
+               wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
+               wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
+
+               $q = new WP_Query( array(
+                       'fields' => 'ids',
+                       'update_post_meta_cache' => false,
+                       'update_post_term_cache' => false,
+                       'tax_query' => array(
+                               'relation' => 'OR',
+                               array(
+                                       'taxonomy' => 'foo',
+                                       'terms' => array( $foo_term_2 ),
+                                       'field' => 'term_id',
+                               ),
+                               array(
+                                       'relation' => 'AND',
+                                       array(
+                                               'taxonomy' => 'foo',
+                                               'terms' => array( $foo_term_1 ),
+                                               'field' => 'term_id',
+                                       ),
+                                       array(
+                                               'taxonomy' => 'bar',
+                                               'terms' => array( $bar_term_1 ),
+                                               'field' => 'term_id',
+                                       ),
+                               ),
+                       ),
+               ) );
+
+               _unregister_taxonomy( 'foo' );
+               _unregister_taxonomy( 'bar' );
+
+               $this->assertEqualSets( array( $p1, $p2 ), $q->posts );
+       }
+
+       /**
+        * @ticket 29738
+        * @group taxonomy
+        */
+       public function test_tax_query_one_double_nested_query_one_first_order_query() {
+               register_taxonomy( 'foo', 'post' );
+               register_taxonomy( 'bar', 'post' );
+
+               $foo_term_1 = $this->factory->term->create( array(
+                       'taxonomy' => 'foo',
+               ) );
+               $foo_term_2 = $this->factory->term->create( array(
+                       'taxonomy' => 'foo',
+               ) );
+               $bar_term_1 = $this->factory->term->create( array(
+                       'taxonomy' => 'bar',
+               ) );
+               $bar_term_2 = $this->factory->term->create( array(
+                       'taxonomy' => 'bar',
+               ) );
+
+               $p1 = $this->factory->post->create();
+               $p2 = $this->factory->post->create();
+               $p3 = $this->factory->post->create();
+               $p4 = $this->factory->post->create();
+
+               wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
+               wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
+               wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
+               wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
+               wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
+               wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
+
+               $q = new WP_Query( array(
+                       'fields' => 'ids',
+                       'update_post_meta_cache' => false,
+                       'update_post_term_cache' => false,
+                       'tax_query' => array(
+                               'relation' => 'OR',
+                               array(
+                                       'taxonomy' => 'foo',
+                                       'terms' => array( $foo_term_2 ),
+                                       'field' => 'term_id',
+                               ),
+                               array(
+                                       'relation' => 'AND',
+                                       array(
+                                               'taxonomy' => 'foo',
+                                               'terms' => array( $foo_term_1 ),
+                                               'field' => 'term_id',
+                                       ),
+                                       array(
+                                               'relation' => 'OR',
+                                               array(
+                                                       'taxonomy' => 'bar',
+                                                       'terms' => array( $bar_term_1 ),
+                                                       'field' => 'term_id',
+                                               ),
+                                               array(
+                                                       'taxonomy' => 'bar',
+                                                       'terms' => array( $bar_term_2 ),
+                                                       'field' => 'term_id',
+                                               ),
+                                       ),
+                               ),
+                       ),
+               ) );
+
+               _unregister_taxonomy( 'foo' );
+               _unregister_taxonomy( 'bar' );
+
+               $this->assertEqualSets( array( $p1, $p2, $p3 ), $q->posts );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 20604
</span><span class="cx" style="display: block; padding: 0 10px">         * @group taxonomy
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1973,7 +2185,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @group taxonomy
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 29718
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 29738
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_populate_taxonomy_query_var_from_tax_query() {
</span><span class="cx" style="display: block; padding: 0 10px">                register_taxonomy( 'foo', 'post' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2088,7 +2300,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @group taxonomy
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 29718
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 29738
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_populate_cat_category_name_query_var_from_tax_query() {
</span><span class="cx" style="display: block; padding: 0 10px">                register_taxonomy( 'foo', 'post' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2096,7 +2308,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'taxonomy' => 'foo',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $c = $this->factory->term->create( array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'taxonomy' => 'foo',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'taxonomy' => 'category',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'slug' => 'bar',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2130,7 +2342,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @group taxonomy
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @ticket 29718
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @ticket 29738
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_populate_tag_id_query_var_from_tax_query() {
</span><span class="cx" style="display: block; padding: 0 10px">                register_taxonomy( 'foo', 'post' );
</span></span></pre>
</div>
</div>

</body>
</html>