diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 21127dc755f1c..5ea5d28a6a8d6 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -1183,7 +1183,8 @@ public function parse_tax_query( &$q ) { continue; // Handled further down in the $q['tag'] block. } - if ( $t->query_var && ! empty( $q[ $t->query_var ] ) ) { + // Values like 0 are not empty strings. See #46350. + if ( $t->query_var && isset( $q[ $t->query_var ] ) && '' !== $q[ $t->query_var ] ) { $tax_query_defaults = array( 'taxonomy' => $taxonomy, 'field' => 'slug', diff --git a/src/wp-includes/class-wp-tax-query.php b/src/wp-includes/class-wp-tax-query.php index 5a489f5662e65..d756c8007c770 100644 --- a/src/wp-includes/class-wp-tax-query.php +++ b/src/wp-includes/class-wp-tax-query.php @@ -604,8 +604,14 @@ public function transform_query( &$query, $resulting_field ) { $resulting_field = sanitize_key( $resulting_field ); - // Empty 'terms' always results in a null transformation. - $terms = array_filter( $query['terms'] ); + // Empty terms always results in a null transformation but terms like 0 are not empty. See #46350. + $terms = array_filter( + $query['terms'], + function ( $value ) { + return 0 !== $value || is_string( $value ); + } + ); + if ( empty( $terms ) ) { $query['terms'] = array(); $query['field'] = $resulting_field; diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 13528c3015c6b..17fc7743006d0 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -1124,4 +1124,34 @@ public function test_default_term_for_post_in_multiple_taxonomies() { $this->assertContains( $tax1, $taxonomies ); $this->assertContains( $tax2, $taxonomies ); } + + /** + * Test taxonomy query with zero (0) term slug. + * + * @ticket 46350 + */ + public function test_taxonomy_query_with_zero_term() { + register_taxonomy( 'test_tax', 'post' ); + + $term_id = self::factory()->term->create( + array( + 'taxonomy' => 'test_tax', + 'name' => '0', + ) + ); + + $posts = self::factory()->post->create_many( 3 ); + + wp_set_object_terms( $posts[0], $term_id, 'test_tax' ); + wp_set_object_terms( $posts[1], $term_id, 'test_tax' ); + + $query = new WP_Query( + array( + 'test_tax' => '0', + 'fields' => 'ids', + ) + ); + + $this->assertCount( 2, $query->posts, 'Posts with the "0" term should be returned.' ); + } }