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
This commit is contained in:
Scott Taylor 2013-12-22 18:30:09 +00:00
parent 9321ee7f29
commit 5cb2ad494e
2 changed files with 33 additions and 2 deletions

View File

@ -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 );

View File

@ -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" ) );