<!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>[26007] trunk: Category and tag are typically checked before checking for a custom taxonomy.</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/26007">26007</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2013-11-04 23:53:55 +0000 (Mon, 04 Nov 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Category and tag are typically checked before checking for a custom taxonomy. If the global query matches category or tag (even if it also has tax_query set), return category/tag as the queried object, instead of arbitrarily returning the first term in the `tax_query` stack (typically those added with 'pre_get_posts').
Real world example: http://www.emusic.com/17dots/topics/daily-download/ - "tag" page, regionalized for US-only content using `pre_get_posts` passing in the terms "US" and "ALL" for "region" (custom tax). All of the theme functions would output "ALL" as the term name. Even though it was a tag archive, the queried object was an arbitrary term from `tax_query`.
See <a href="http://core.trac.wordpress.org/changeset/26006">[26006]</a>. All unit tests pass.
Fixes <a href="http://core.trac.wordpress.org/ticket/20767">#20767</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesqueryphp">trunk/src/wp-includes/query.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsquerytaxQueryphp">trunk/tests/phpunit/tests/query/taxQuery.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesqueryphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/query.php (26006 => 26007)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/query.php 2013-11-04 22:46:44 UTC (rev 26006)
+++ trunk/src/wp-includes/query.php 2013-11-04 23:53:55 UTC (rev 26007)
</span><span class="lines">@@ -3248,20 +3248,25 @@
</span><span class="cx"> $this->queried_object_id = 0;
</span><span class="cx">
</span><span class="cx"> if ( $this->is_category || $this->is_tag || $this->is_tax ) {
</span><del>- $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
</del><ins>+ if ( $this->is_category ) {
+ $term = get_term( $this->get( 'cat' ), 'category' );
+ } elseif ( $this->is_tag ) {
+ $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
+ } else {
+ $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
+ $query = reset( $tax_query_in_and );
</ins><span class="cx">
</span><del>- $query = reset( $tax_query_in_and );
</del><ins>+ if ( 'term_id' == $query['field'] )
+ $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
+ else
+ $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
+ }
</ins><span class="cx">
</span><del>- if ( 'term_id' == $query['field'] )
- $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
- elseif ( $query['terms'] )
- $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
-
</del><span class="cx"> if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
</span><span class="cx"> $this->queried_object = $term;
</span><span class="cx"> $this->queried_object_id = (int) $term->term_id;
</span><span class="cx">
</span><del>- if ( $this->is_category )
</del><ins>+ if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
</ins><span class="cx"> _make_cat_compat( $this->queried_object );
</span><span class="cx"> }
</span><span class="cx"> } elseif ( $this->is_post_type_archive ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsquerytaxQueryphp"></a>
<div class="addfile"><h4>Added: trunk/tests/phpunit/tests/query/taxQuery.php (0 => 26007)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/query/taxQuery.php (rev 0)
+++ trunk/tests/phpunit/tests/query/taxQuery.php 2013-11-04 23:53:55 UTC (rev 26007)
</span><span class="lines">@@ -0,0 +1,136 @@
</span><ins>+<?php
+
+/**
+ * Test the is_*() functions in query.php across the URL structure
+ *
+ * This exercises both query.php and rewrite.php: urls are fed through the rewrite code,
+ * then we test the effects of each url on the wp_query object.
+ *
+ * @group query
+ * @group rewrite
+ */
+class Tests_Query_TaxQuery extends WP_UnitTestCase {
+ protected $tag_id;
+ protected $cat_id;
+ protected $tax_id;
+ protected $tax_id2;
+ protected $post_id;
+
+ protected $cat;
+ protected $tag;
+ protected $tax;
+
+ function setUp() {
+ parent::setUp();
+
+ set_current_screen( 'front' );
+
+ update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
+
+ $GLOBALS['wp_the_query'] = new WP_Query();
+ $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
+
+ create_initial_taxonomies();
+ register_taxonomy( 'testtax', 'post', array( 'public' => true ) );
+
+ $GLOBALS['wp_rewrite']->init();
+ flush_rewrite_rules();
+
+ $this->tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) );
+ $this->cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) );
+ $this->tax_id = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug' ) );
+ $this->tax_id2 = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug2' ) );
+ $this->post_id = $this->factory->post->create();
+ wp_set_object_terms( $this->post_id, $this->cat_id, 'category' );
+ wp_set_object_terms( $this->post_id, array( $this->tax_id, $this->tax_id2 ), 'testtax' );
+
+ $this->cat = get_term( $this->cat_id, 'category' );
+ _make_cat_compat( $this->cat );
+ $this->tag = get_term( $this->tag_id, 'post_tag' );
+
+ add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
+ }
+
+ function tearDown() {
+ parent::tearDown();
+
+ _unregister_taxonomy( 'testtax' );
+
+ remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
+ }
+
+ function test_tag_action_tax() {
+ // tag with tax added
+ $this->go_to( home_url( "/tag/tag-slug/" ) );
+ $this->assertQueryTrue( 'is_tag', 'is_archive' );
+ $this->assertNotEmpty( get_query_var( 'tax_query' ) );
+ $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
+ $this->assertNotEmpty( get_query_var( 'term_id' ) );
+ $this->assertNotEmpty( get_query_var( 'tag_id' ) );
+ $this->assertEquals( get_queried_object(), $this->tag );
+ }
+
+ function test_tag_query_cat_action_tax() {
+ // tag + category with tax added
+ $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id" ) );
+ $this->assertQueryTrue( 'is_category', 'is_tag', 'is_archive' );
+ $this->assertNotEmpty( get_query_var( 'tax_query' ) );
+ $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
+ $this->assertNotEmpty( get_query_var( 'term_id' ) );
+ $this->assertNotEmpty( get_query_var( 'cat' ) );
+ $this->assertNotEmpty( get_query_var( 'tag_id' ) );
+ $this->assertEquals( get_queried_object(), $this->cat );
+ }
+
+ function test_tag_query_cat_query_tax_action_tax() {
+ // tag + category + tax with tax added
+ $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id&testtax=tax-slug2" ) );
+ $this->assertQueryTrue( 'is_category', 'is_tag', 'is_tax', 'is_archive' );
+ $this->assertNotEmpty( get_query_var( 'tax_query' ) );
+ $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
+ $this->assertNotEmpty( get_query_var( 'term_id' ) );
+ $this->assertNotEmpty( get_query_var( 'cat' ) );
+ $this->assertNotEmpty( get_query_var( 'tag_id' ) );
+ $this->assertNotEmpty( get_query_var( 'testtax' ) );
+ $this->assertEquals( get_queried_object(), $this->cat );
+ }
+
+ function test_cat_action_tax() {
+ // category with tax added
+ $this->go_to( home_url( "/category/cat-slug/" ) );
+ $this->assertQueryTrue( 'is_category', 'is_archive' );
+ $this->assertNotEmpty( get_query_var( 'cat' ) );
+ $this->assertNotEmpty( get_query_var( 'tax_query' ) );
+ $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
+ $this->assertNotEmpty( get_query_var( 'term_id' ) );
+ $this->assertEquals( get_queried_object(), $this->cat );
+ }
+
+ function test_tax_query_tag_action_tax() {
+ // tax + tag with tax added
+ $this->go_to( home_url( "/testtax/tax-slug2/?tag_id=$this->tag_id" ) );
+ $this->assertQueryTrue( 'is_tag', 'is_tax', 'is_archive' );
+ $this->assertNotEmpty( get_query_var( 'tax_query' ) );
+ $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
+ $this->assertNotEmpty( get_query_var( 'term_id' ) );
+ $this->assertNotEmpty( get_query_var( 'tag_id' ) );
+ $this->assertEquals( get_queried_object(), $this->tag );
+ }
+
+ function test_tax_query_cat_action_tax() {
+ // tax + cat with tax added
+ $this->go_to( home_url( "/testtax/tax-slug2/?cat=$this->cat_id" ) );
+ $this->assertQueryTrue( 'is_category', 'is_tax', 'is_archive' );
+ $this->assertNotEmpty( get_query_var( 'tax_query' ) );
+ $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
+ $this->assertNotEmpty( get_query_var( 'term_id' ) );
+ $this->assertNotEmpty( get_query_var( 'cat' ) );
+ $this->assertEquals( get_queried_object(), $this->cat );
+ }
+
+ function pre_get_posts_tax_category_tax_query( &$query ) {
+ $query->set( 'tax_query', array(
+ array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id )
+ ) );
+ }
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>
</body>
</html>