<!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>[28562] trunk: When adding queries to `tax_query`: if the query's `field` is `term_taxonomy_id`, don't require `taxonomy` to be specified.</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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/28562">28562</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-05-23 19:58:52 +0000 (Fri, 23 May 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>When adding queries to `tax_query`: if the query's `field` is `term_taxonomy_id`, don't require `taxonomy` to be specified. In `WP_Tax_Query::transform_query()`, `$query['taxonomy']` is never checked for the 'term_taxonomy_id' `case` because 'term_taxonomy_id' is the primary key being looked up.

Adds unit tests.

Props helen.
Fixes <a href="http://core.trac.wordpress.org/ticket/25284">#25284</a>.</pre>

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/taxonomy.php (28561 => 28562)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/taxonomy.php       2014-05-23 19:28:45 UTC (rev 28561)
+++ trunk/src/wp-includes/taxonomy.php  2014-05-23 19:58:52 UTC (rev 28562)
</span><span class="lines">@@ -632,15 +632,15 @@
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="cx">   * List of taxonomy queries. A single taxonomy query is an associative array:
</span><del>-        * - 'taxonomy' string The taxonomy being queried
</del><ins>+         * - 'taxonomy' string The taxonomy being queried. Optional when using the term_taxonomy_id field.
</ins><span class="cx">    * - 'terms' string|array The list of terms
</span><span class="cx">   * - 'field' string (optional) Which term field is being used.
</span><del>-        *              Possible values: 'term_id', 'slug' or 'name'
</del><ins>+         *              Possible values: 'term_id', 'slug', 'name', or 'term_taxonomy_id'
</ins><span class="cx">    *              Default: 'term_id'
</span><span class="cx">   * - 'operator' string (optional)
</span><span class="cx">   *              Possible values: 'AND', 'IN' or 'NOT IN'.
</span><span class="cx">   *              Default: 'IN'
</span><del>-        * - 'include_children' bool (optional) Whether to include child terms.
</del><ins>+         * - 'include_children' bool (optional) Whether to include child terms. Requires that a taxonomy be specified.
</ins><span class="cx">    *              Default: true
</span><span class="cx">   *
</span><span class="cx">   * @since 3.1.0
</span><span class="lines">@@ -818,7 +818,15 @@
</span><span class="cx">   * @param array &$query The single query
</span><span class="cx">   */
</span><span class="cx">  private function clean_query( &$query ) {
</span><del>-               if ( ! taxonomy_exists( $query['taxonomy'] ) ) {
</del><ins>+                if ( empty( $query['taxonomy'] ) ) {
+                       if ( 'term_taxonomy_id' !== $query['field'] ) {
+                               $query = new WP_Error( 'Invalid taxonomy' );
+                               return;
+                       }
+
+                       // so long as there are shared terms, include_children requires that a taxonomy is set
+                       $query['include_children'] = false;
+               } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) {
</ins><span class="cx">                   $query = new WP_Error( 'Invalid taxonomy' );
</span><span class="cx">                  return;
</span><span class="cx">          }
</span></span></pre></div>
<a id="trunktestsphpunitteststermqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/term/query.php (28561 => 28562)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/term/query.php 2014-05-23 19:28:45 UTC (rev 28561)
+++ trunk/tests/phpunit/tests/term/query.php    2014-05-23 19:58:52 UTC (rev 28562)
</span><span class="lines">@@ -98,4 +98,108 @@
</span><span class="cx">          $ids = $query->get_posts();
</span><span class="cx">          $this->assertEquals( array( $post_id1, $post_id2 ), $ids );
</span><span class="cx">  }
</span><del>-}
</del><span class="cx">\ No newline at end of file
</span><ins>+
+       function test_tax_query_no_taxonomy() {
+               $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) );
+               $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) );
+
+               $response1 = new WP_Query( array(
+                       'tax_query' => array(
+                               array( 'terms' => array( $cat_id ) )
+                       )
+               ) );
+               $this->assertEmpty( $response1->posts );
+
+               $response2 = new WP_Query( array(
+                       'tax_query' => array(
+                               array(
+                                       'taxonomy' => 'category',
+                                       'terms' => array( $cat_id )
+                               )
+                       )
+               ) );
+               $this->assertNotEmpty( $response2->posts );
+
+               $term = get_category( $cat_id );
+               $response3 = new WP_Query( array(
+                       'tax_query' => array(
+                               array(
+                                       'field' => 'term_taxonomy_id',
+                                       'terms' => array( $term->term_taxonomy_id )
+                               )
+                       )
+               ) );
+               $this->assertNotEmpty( $response3->posts );
+       }
+
+       function test_term_taxonomy_id_field_no_taxonomy() {
+               $posts = $this->factory->post->create_many( 5 );
+
+               $cats = $tags = array();
+
+               // need term_taxonomy_ids in addition to term_ids, so no factory
+               for ( $i = 0; $i < 5; $i++ ) {
+                       $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );
+                       $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );
+
+                       // post 0 gets all terms
+                       wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );
+                       wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );
+               }
+
+               wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
+               wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
+
+               wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
+               wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
+
+               wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
+               wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
+
+               $results1 = $this->q->query( array(
+                       'fields' => 'ids',
+                       'orderby' => 'id',
+                       'order' => 'ASC',
+                       'tax_query' => array(
+                               'relation' => 'OR',
+                               array(
+                                       'field' => 'term_taxonomy_id',
+                                       'terms' => array( $cats[0]['term_taxonomy_id'], $cats[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'], $tags[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'] ),
+                                       'operator' => 'AND',
+                                       'include_children' => false,
+                               ),
+                               array(
+                                       'field' => 'term_taxonomy_id',
+                                       'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
+                                       'operator' => 'AND',
+                                       'include_children' => false,
+                               )
+                       )
+               ) );
+
+               $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );
+
+               $results2 = $this->q->query( array(
+                       'fields' => 'ids',
+                       'orderby' => 'id',
+                       'order' => 'ASC',
+                       'tax_query' => array(
+                               'relation' => 'AND',
+                               array(
+                                       'field' => 'term_taxonomy_id',
+                                       'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
+                                       'operator' => 'IN',
+                                       'include_children' => false,
+                               ),
+                               array(
+                                       'field' => 'term_taxonomy_id',
+                                       'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
+                                       'operator' => 'IN',
+                                       'include_children' => false,
+                               )
+                       )
+               ) );
+
+               $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>