diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index bd2ebc11f6..cfb1e772a2 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -3869,14 +3869,18 @@ class WP_Query { $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 ); + // For other tax queries, grab the first term from the first clause. + $tax_query_in_and = wp_list_filter( $this->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' ); + + $queried_taxonomies = array_keys( $tax_query_in_and ); + $matched_taxonomy = reset( $queried_taxonomies ); + $query = $tax_query_in_and[ $matched_taxonomy ]; if ( $query['terms'] ) { if ( 'term_id' == $query['field'] ) { - $term = get_term( reset( $query['terms'] ), $query['taxonomy'] ); + $term = get_term( reset( $query['terms'] ), $matched_taxonomy ); } else { - $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] ); + $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy ); } } } diff --git a/tests/phpunit/tests/query/taxQuery.php b/tests/phpunit/tests/query/taxQuery.php index fe6540f808..61b08d951c 100644 --- a/tests/phpunit/tests/query/taxQuery.php +++ b/tests/phpunit/tests/query/taxQuery.php @@ -180,4 +180,103 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id ) ) ); } -} \ No newline at end of file + + /** + * @group 30623 + */ + public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_term_id_should_return_term_object() { + // Don't override the args provided below. + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + + $args = array( + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'testtax', + 'field' => 'term_id', + 'terms' => array( + $this->tax_id, + ), + ), + ) + ); + + $q = new WP_Query( $args ); + $object = $q->get_queried_object(); + + $expected = get_term( $this->tax_id, 'testtax' ); + + $this->assertEquals( $expected, $object ); + } + + /** + * @group 30623 + */ + public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_slug_should_return_term_object() { + // Don't override the args provided below. + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + + $args = array( + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'testtax', + 'field' => 'slug', + 'terms' => array( + 'tax-slug', + ), + ), + ) + ); + + $q = new WP_Query( $args ); + $object = $q->get_queried_object(); + + $expected = get_term( $this->tax_id, 'testtax' ); + + // Only compare term_id because object_id may or may not be part of either value. + $this->assertEquals( $expected->term_id, $object->term_id ); + } + + /** + * @group 30623 + */ + public function test_get_queried_object_with_custom_taxonomy_tax_query_with_multiple_clauses_should_return_term_object_corresponding_to_the_first_queried_tax() { + // Don't override the args provided below. + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + + register_taxonomy( 'testtax2', 'post' ); + $testtax2_term_id = $this->factory->term->create( array( + 'taxonomy' => 'testtax2', + 'slug' => 'testtax2-slug', + ) ); + + $args = array( + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'testtax', + 'field' => 'slug', + 'terms' => array( + 'tax-slug', + ), + ), + array( + 'taxonomy' => 'testtax2', + 'field' => 'slug', + 'terms' => array( + 'testtax2-slug', + ), + ), + ) + ); + + $q = new WP_Query( $args ); + $object = $q->get_queried_object(); + + $expected = get_term( $this->tax_id, 'testtax' ); + + // Only compare term_id because object_id may or may not be part of either value. + $this->assertEquals( $expected->term_id, $object->term_id ); + } +}