From ca85591c3c87327eaf96ccdbc88cba009eb6aba6 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 4 Nov 2013 23:54:10 +0000 Subject: [PATCH] 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 [26006]. All unit tests pass. Fixes #20767. Built from https://develop.svn.wordpress.org/trunk@26007 git-svn-id: http://core.svn.wordpress.org/trunk@25938 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 93d57c2db8..5ef51b50a1 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -3248,20 +3248,25 @@ class WP_Query { $this->queried_object_id = 0; if ( $this->is_category || $this->is_tag || $this->is_tax ) { - $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' ); + 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 ); - $query = reset( $tax_query_in_and ); - - 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'] ); + 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'] ); + } if ( ! empty( $term ) && ! is_wp_error( $term ) ) { $this->queried_object = $term; $this->queried_object_id = (int) $term->term_id; - if ( $this->is_category ) + if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) _make_cat_compat( $this->queried_object ); } } elseif ( $this->is_post_type_archive ) {