In `WP_Query::get_queried_object()`, use the new format for referencing tax query clauses.

`queried_terms`, rather than `queries`, is the tax_query property where a flat
index of terms is stored.

See [29901] for a similar fix in `redirect_canonical()`. See #29738.

Props dd32.
Fixes #30623.

git-svn-id: https://develop.svn.wordpress.org/trunk@30771 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-12-07 14:57:53 +00:00
parent 2f25ec0773
commit 98d17497d2
2 changed files with 108 additions and 5 deletions

View File

@ -3869,14 +3869,18 @@ class WP_Query {
$term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' ); $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
} }
} else { } else {
$tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' ); // For other tax queries, grab the first term from the first clause.
$query = reset( $tax_query_in_and ); $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 ( $query['terms'] ) {
if ( 'term_id' == $query['field'] ) { if ( 'term_id' == $query['field'] ) {
$term = get_term( reset( $query['terms'] ), $query['taxonomy'] ); $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
} else { } else {
$term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] ); $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
} }
} }
} }

View File

@ -180,4 +180,103 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase {
array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id ) array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id )
) ); ) );
} }
/**
* @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 );
}
} }