diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index d523bcc351..d060b7e3b6 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -1830,6 +1830,8 @@ class WP_Query { 'field' => 'term_id', 'include_children' => false ); + } elseif ( isset( $this->query['category__in'] ) ) { + $q['category__in'] = false; } if ( ! empty($q['category__not_in']) ) { @@ -1887,6 +1889,8 @@ class WP_Query { 'taxonomy' => 'post_tag', 'terms' => $q['tag__in'] ); + } elseif ( isset( $this->query['tag__in'] ) ) { + $q['tag__in'] = false; } if ( !empty($q['tag__not_in']) ) { @@ -1914,6 +1918,8 @@ class WP_Query { 'terms' => $q['tag_slug__in'], 'field' => 'slug' ); + } elseif ( isset( $this->query['tag_slug__in'] ) ) { + $q['tag_slug__in'] = false; } if ( !empty($q['tag_slug__and']) ) { @@ -2500,6 +2506,13 @@ class WP_Query { $where .= $clauses['where']; } + // If *__in is passed to WP_Query as an empty array, don't return results + foreach ( array( 'category', 'tag', 'tag_slug' ) as $in ) { + if ( isset( $q["{$in}__in"] ) && false === $q["{$in}__in"] ) { + $where = " AND 1=0 $where"; + } + } + if ( $this->is_tax ) { if ( empty($post_type) ) { // Do a fully inclusive search for currently registered post types of queried taxonomies @@ -2591,6 +2604,9 @@ class WP_Query { } elseif ( ! empty( $q['author__in'] ) ) { $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; + } elseif ( isset( $this->query['author__in'] ) ) { + $author__in = 0; + $where .= ' AND 1=0 '; } // Author stuff for nice URLs diff --git a/tests/phpunit/tests/query/results.php b/tests/phpunit/tests/query/results.php index 9c4ce065f9..08e8e1900f 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -513,6 +513,9 @@ class Tests_Query_Results extends WP_UnitTestCase { $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) ); $this->assertEqualSets( array( $author_1, $author_2 ), $author_ids ); + $posts = $this->q->query( array( 'author__in' => array() ) ); + $this->assertEmpty( $posts ); + $posts = $this->q->query( array( 'author__not_in' => array( $author_1, $author_2 ), 'post__in' => array( $post_1, $post_2, $post_3, $post_4 ) diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 7cfa26b07d..62f277de6d 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -202,4 +202,30 @@ class Tests_Tax_Query extends WP_UnitTestCase { $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' ); } + + function test_empty__in() { + $cat_id = $this->factory->category->create(); + $post_id = $this->factory->post->create(); + wp_set_post_categories( $post_id, $cat_id ); + + $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) ); + $this->assertNotEmpty( $q1 ); + $q2 = get_posts( array( 'category__in' => array() ) ); + $this->assertEmpty( $q2 ); + + $tag = wp_insert_term( 'woo', 'post_tag' ); + $tag_id = $tag['term_id']; + $slug = get_tag( $tag_id )->slug; + wp_set_post_tags( $post_id, $slug ); + + $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) ); + $this->assertNotEmpty( $q3 ); + $q4 = get_posts( array( 'tag__in' => array() ) ); + $this->assertEmpty( $q4 ); + + $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) ); + $this->assertNotEmpty( $q5 ); + $q6 = get_posts( array( 'tag_slug__in' => array() ) ); + $this->assertEmpty( $q6 ); + } }