From 0673904ddfef76bb928f06e1b6c1a21f4d680f02 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 6 Feb 2016 03:57:33 +0000 Subject: [PATCH] `WP_Query` taxonomy query vars should be set to first of multiple taxonomies. This provides better parity with `get_queried_object()`, which will return the first taxonomy/term matched by the current query. [29891] introduced the abnormal behavior for the 'taxonomy' and 'term' query vars. Props Chouby. Fixes #35619. git-svn-id: https://develop.svn.wordpress.org/trunk@36484 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 3 +++ tests/phpunit/tests/query.php | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 982b92e97a..ad407c1fdd 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2857,6 +2857,9 @@ class WP_Query { } else { $q['term_id'] = $queried_items['terms'][0]; } + + // Take the first one we find. + break; } } } diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 459b5a40f3..48086b0bfe 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -522,4 +522,44 @@ class Tests_Query extends WP_UnitTestCase { $this->assertSame( array( $p2 ), $q->posts ); } + + /** + * @ticket 35619 + */ + public function test_get_queried_object_should_return_first_of_multiple_terms() { + register_taxonomy( 'tax1', 'post' ); + register_taxonomy( 'tax2', 'post' ); + + $term1 = $this->factory->term->create( array( 'taxonomy' => 'tax1', 'name' => 'term1' ) ); + $term2 = $this->factory->term->create( array( 'taxonomy' => 'tax2', 'name' => 'term2' ) ); + $post_id = $this->factory->post->create(); + wp_set_object_terms( $post_id, 'term1', 'tax1' ); + wp_set_object_terms( $post_id, 'term2', 'tax2' ); + + $this->go_to( home_url( '?tax1=term1&tax2=term2' ) ); + $queried_object = get_queried_object(); + + $this->assertSame( 'tax1', $queried_object->taxonomy ); + $this->assertSame( 'term1', $queried_object->slug ); + } + + /** + * @ticket 35619 + */ + public function test_query_vars_should_match_first_of_multiple_terms() { + register_taxonomy( 'tax1', 'post' ); + register_taxonomy( 'tax2', 'post' ); + + $term1 = $this->factory->term->create( array( 'taxonomy' => 'tax1', 'name' => 'term1' ) ); + $term2 = $this->factory->term->create( array( 'taxonomy' => 'tax2', 'name' => 'term2' ) ); + $post_id = $this->factory->post->create(); + wp_set_object_terms( $post_id, 'term1', 'tax1' ); + wp_set_object_terms( $post_id, 'term2', 'tax2' ); + + $this->go_to( home_url( '?tax1=term1&tax2=term2' ) ); + $queried_object = get_queried_object(); + + $this->assertSame( 'tax1', get_query_var( 'taxonomy' ) ); + $this->assertSame( 'term1', get_query_var( 'term' ) ); + } }