From 6adcd154358a16f876c2d386d86af655c752c141 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 9 Sep 2016 19:36:22 +0000 Subject: [PATCH] Query: Avoid PHP notice in `get_queried_object()` when query contains `NOT EXISTS` tax query. Props johnjamesjacoby. See #37962. git-svn-id: https://develop.svn.wordpress.org/trunk@38585 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-query.php | 2 +- tests/phpunit/tests/query.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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;