diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 63b505417f..dcf1b56b37 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1041,7 +1041,7 @@ class WP_Tax_Query { } elseif ( 'NOT IN' == $operator ) { if ( empty( $terms ) ) { - continue; + return $sql; } $terms = implode( ',', $terms ); @@ -1055,7 +1055,7 @@ class WP_Tax_Query { } elseif ( 'AND' == $operator ) { if ( empty( $terms ) ) { - continue; + return $sql; } $num_terms = count( $terms ); diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 70f3987653..57dac3610c 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -352,4 +352,57 @@ class Tests_Tax_Query extends WP_UnitTestCase { _unregister_taxonomy( 'wptests_tax' ); } + /** + * @ticket 29738 + */ + public function test_get_sql_operator_not_in_empty_terms() { + register_taxonomy( 'wptests_tax', 'post' ); + + $tq = new WP_Tax_Query( array( + 'relation' => 'OR', + array( + 'taxonomy' => 'wptests_tax', + 'field' => 'term_id', + 'operator' => 'NOT IN', + 'terms' => array(), + ), + ) ); + + global $wpdb; + $expected = array( + 'join' => '', + 'where' => '', + ); + + $this->assertSame( $expected, $tq->get_sql( $wpdb->posts, 'ID' ) ); + + _unregister_taxonomy( 'wptests_tax' ); + } + + /** + * @ticket 29738 + */ + public function test_get_sql_operator_and_empty_terms() { + register_taxonomy( 'wptests_tax', 'post' ); + + $tq = new WP_Tax_Query( array( + 'relation' => 'OR', + array( + 'taxonomy' => 'wptests_tax', + 'field' => 'term_id', + 'operator' => 'AND', + 'terms' => array(), + ), + ) ); + + global $wpdb; + $expected = array( + 'join' => '', + 'where' => '', + ); + + $this->assertSame( $expected, $tq->get_sql( $wpdb->posts, 'ID' ) ); + + _unregister_taxonomy( 'wptests_tax' ); + } }