From 5cb2ad494e819a4b48cd9a8cf7f84b00011d59d1 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sun, 22 Dec 2013 18:30:09 +0000 Subject: [PATCH] Fix a regression for `get_queried_object()` by checking for `category_name` when `cat` isn't set - mainly `is_category()` being true for Uncategorized or when queried object is accessed in `pre_get_posts`. Also check for `$query['terms']` when trying to assign a term as the queried object when `is_tax()` is true. Adds a unit test. See [26007] for how I originally broke this while fixing a bigger issue. Props Chouby, jeremyfelt. Fixes #26634, #26627. git-svn-id: https://develop.svn.wordpress.org/trunk@26864 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 8 ++++++-- tests/phpunit/tests/query/taxQuery.php | 27 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 1ec9566c41..d64a25a709 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -3264,10 +3264,14 @@ class WP_Query { if ( $this->is_category || $this->is_tag || $this->is_tax ) { if ( $this->is_category ) { - $term = get_term( $this->get( 'cat' ), 'category' ); + if ( $this->get( 'cat' ) ) { + $term = get_term( $this->get( 'cat' ), 'category' ); + } elseif ( $this->get( 'category_name' ) ) { + $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' ); + } } elseif ( $this->is_tag ) { $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); - } else { + } elseif ( $query['terms'] ) { $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' ); $query = reset( $tax_query_in_and ); diff --git a/tests/phpunit/tests/query/taxQuery.php b/tests/phpunit/tests/query/taxQuery.php index d2554d721d..738d3abfbf 100644 --- a/tests/phpunit/tests/query/taxQuery.php +++ b/tests/phpunit/tests/query/taxQuery.php @@ -17,6 +17,7 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { protected $post_id; protected $cat; + protected $uncat; protected $tag; protected $tax; @@ -48,6 +49,9 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { _make_cat_compat( $this->cat ); $this->tag = get_term( $this->tag_id, 'post_tag' ); + $this->uncat = get_term_by( 'slug', 'uncategorized', 'category' ); + _make_cat_compat( $this->uncat ); + add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); } @@ -106,6 +110,29 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { $this->assertEquals( get_queried_object(), $this->cat ); } + function test_cat_uncat_action_tax() { + // category with tax added + add_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); + + $this->go_to( home_url( "/category/uncategorized/" ) ); + $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->uncat ); + + remove_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); + } + + function _cat_uncat_action_tax( &$query ) { + $this->assertTrue( $query->is_category() ); + $this->assertTrue( $query->is_archive() ); + $this->assertNotEmpty( $query->get( 'category_name' ) ); + $this->assertNotEmpty( $query->get( 'tax_query' ) ); + $this->assertEquals( $query->get_queried_object(), $this->uncat ); + } + 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" ) );