diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 95904060ce..fd7dfe08db 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3264,7 +3264,7 @@ class WP_Query { $matched_taxonomy = reset( $queried_taxonomies ); $query = $tax_query_in_and[ $matched_taxonomy ]; - if ( $query['terms'] ) { + if ( ! empty( $query['terms'] ) ) { if ( 'term_id' == $query['field'] ) { $term = get_term( reset( $query['terms'] ), $matched_taxonomy ); } else { diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 48086b0bfe..04e6d092f2 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -118,6 +118,25 @@ class Tests_Query extends WP_UnitTestCase { unset( $q->query_vars['wptests_tax'] ); } + /** + * @ticket 37962 + */ + public function test_get_queried_object_should_return_null_for_not_exists_tax_query() { + register_taxonomy( 'wptests_tax', 'post' ); + + $q = new WP_Query( array( + 'tax_query' => array( + array( + 'taxonomy' => 'wptests_tax', + 'operator' => 'NOT EXISTS', + ), + ), + ) ); + + $queried_object = $q->get_queried_object(); + $this->assertNull( $queried_object ); + } + public function test_orderby_space_separated() { global $wpdb;