From 0923b7b93aa625720f7cad2350a17ea66b098c8d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 4 Nov 2013 23:53:55 +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. git-svn-id: https://develop.svn.wordpress.org/trunk@26007 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 21 ++-- tests/phpunit/tests/query/taxQuery.php | 136 +++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 8 deletions(-) create mode 100644 tests/phpunit/tests/query/taxQuery.php diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 93d57c2db8..5ef51b50a1 100644 --- a/src/wp-includes/query.php +++ b/src/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 ) { diff --git a/tests/phpunit/tests/query/taxQuery.php b/tests/phpunit/tests/query/taxQuery.php new file mode 100644 index 0000000000..d2554d721d --- /dev/null +++ b/tests/phpunit/tests/query/taxQuery.php @@ -0,0 +1,136 @@ + 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 ) + ) ); + } +} \ No newline at end of file