In `WP_Query::get_queried_object()`, avoid PHP notices when `is_tax` is paired with an empty `tax_query`.
It's possible to have an empty `tax_query` and `is_tax=true` when the initial query contains a taxonomy var (and is processed as such during `WP_Query::parse_query()`) but the taxonomy var is unset during a 'parse_query' callback. While this kind of behavior is not necessarily something we need to support, we should continue to avoid PHP notices in such cases, as we did prior to WP 4.1. Fixes #31246. git-svn-id: https://develop.svn.wordpress.org/trunk@31366 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
fa1a382dff
commit
bdda122e7f
|
@ -3900,6 +3900,7 @@ class WP_Query {
|
|||
// 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' );
|
||||
|
||||
if ( ! empty( $tax_query_in_and ) ) {
|
||||
$queried_taxonomies = array_keys( $tax_query_in_and );
|
||||
$matched_taxonomy = reset( $queried_taxonomies );
|
||||
$query = $tax_query_in_and[ $matched_taxonomy ];
|
||||
|
@ -3912,6 +3913,7 @@ class WP_Query {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
|
||||
$this->queried_object = $term;
|
||||
|
|
|
@ -91,4 +91,35 @@ class Tests_Query extends WP_UnitTestCase {
|
|||
$this->assertCount( 1, $query->get( 'tag_slug__in' ) );
|
||||
$this->assertEquals( $query->get_queried_object(), $tag );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 31246
|
||||
*/
|
||||
public function test_get_queried_object_should_return_null_when_is_tax_is_true_but_the_taxonomy_args_have_been_removed_in_a_parse_query_callback() {
|
||||
// Don't override the args provided below.
|
||||
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 2, array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
) );
|
||||
|
||||
$posts = $this->factory->post->create_many( 2 );
|
||||
|
||||
wp_set_object_terms( $posts[0], array( $terms[0] ), 'wptests_tax' );
|
||||
wp_set_object_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
|
||||
|
||||
add_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
|
||||
$q = new WP_Query( array(
|
||||
'fields' => 'ids',
|
||||
'wptests_tax' => $terms[1],
|
||||
) );
|
||||
|
||||
remove_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
|
||||
|
||||
$this->assertNull( $q->get_queried_object() );
|
||||
}
|
||||
|
||||
public function filter_parse_query_to_remove_tax( $q ) {
|
||||
unset( $q->query_vars['wptests_tax'] );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue