diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 13c0ea3f80..a7d400daa8 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -326,6 +326,9 @@ class WP_Term_Query { $has_hierarchical_tax = true; } } + } else { + // When no taxonomies are provided, assume we have to descend the tree. + $has_hierarchical_tax = true; } if ( ! $has_hierarchical_tax ) { diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 4daac1c35f..699330851b 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -767,4 +767,83 @@ class Tests_Term_Query extends WP_UnitTestCase { public static function filter_terms_pre_query( $terms, $query ) { return array( 555 ); } + + /** + * @ticket 37728 + */ + public function test_hide_empty_should_include_empty_parents_of_nonempty_children() { + register_taxonomy( + 'wptests_tax', + 'post', + array( + 'hierarchical' => true, + ) + ); + + $t1 = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + ) + ); + + $t2 = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'parent' => $t1, + ) + ); + + $p = self::factory()->post->create(); + + wp_set_object_terms( $p, $t2, 'wptests_tax' ); + + $q = new WP_Term_Query( + array( + 'taxonomy' => 'wptests_tax', + 'hide_empty' => true, + 'fields' => 'ids', + ) + ); + + $this->assertContains( $t1, $q->terms ); + } + + /** + * @ticket 37728 + */ + public function test_hide_empty_should_include_empty_parents_of_nonempty_children_when_category_is_unspecified() { + register_taxonomy( + 'wptests_tax', + 'post', + array( + 'hierarchical' => true, + ) + ); + + $t1 = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + ) + ); + + $t2 = self::factory()->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'parent' => $t1, + ) + ); + + $p = self::factory()->post->create(); + + wp_set_object_terms( $p, $t2, 'wptests_tax' ); + + $q = new WP_Term_Query( + array( + 'hide_empty' => true, + 'fields' => 'ids', + ) + ); + + $this->assertContains( $t1, $q->terms ); + } }