From 36df368171520550ea3aa13ae483fe46c1a5a481 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 12 Mar 2014 04:07:41 +0000 Subject: [PATCH] In `WP_Query::get_queried_object()`, account for `pre_get_posts` by checking for `tag` when `tag_id` isn't present. Tags still need to be rolled up into `tax_query`. Add a unit test confirming expected query vars during and after `pre_get_posts`. Props mattonomics for a patch. See #27362. git-svn-id: https://develop.svn.wordpress.org/trunk@27511 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 6 +++++- tests/phpunit/tests/query.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 0f7e104f99..25436d96fb 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -3663,7 +3663,11 @@ class WP_Query { $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' ); } } elseif ( $this->is_tag ) { - $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); + if ( $this->get( 'tag_id' ) ) { + $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); + } elseif ( $this->get( 'tag' ) ) { + $term = get_term_by( 'slug', $this->get( 'tag' ), '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 ); diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 6207d85219..60d69cf230 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -104,4 +104,35 @@ class Tests_Query extends WP_UnitTestCase { function filter_posts_per_page( &$query ) { $query->set( 'posts_per_rss', 30 ); } + + /** + * @ticket 26627 + */ + function test_tag_queried_object() { + $slug = 'tag-slug-26627'; + $this->factory->tag->create( array( 'slug' => $slug ) ); + $tag = get_term_by( 'slug', $slug, 'post_tag' ); + + add_action( 'pre_get_posts', array( $this, '_tag_queried_object' ), 11 ); + + $this->go_to( get_term_link( $tag ) ); + + $this->assertQueryTrue( 'is_tag', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'tag_id' ) ); + $this->assertNotEmpty( get_query_var( 'tag' ) ); + $this->assertEmpty( get_query_var( 'tax_query' ) ); + $this->assertCount( 1, get_query_var( 'tag_slug__in' ) ); + $this->assertEquals( get_queried_object(), $tag ); + + remove_action( 'pre_get_posts', array( $this, '_tag_queried_object' ), 11 ); + } + + function _tag_queried_object( &$query ) { + $tag = get_term_by( 'slug', 'tag-slug-26627', 'post_tag' ); + $this->assertTrue( $query->is_tag() ); + $this->assertTrue( $query->is_archive() ); + $this->assertNotEmpty( $query->get( 'tag' ) ); + $this->assertCount( 1, $query->get( 'tag_slug__in' ) ); + $this->assertEquals( $query->get_queried_object(), $tag ); + } } \ No newline at end of file