From 3e704473e709479f680f3e93747cbc9a17203663 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 23 Aug 2019 16:04:07 +0000 Subject: [PATCH] Taxonomy: Ensure consistency of `hide_empty` in term queries when `taxonomy` is excluded. When querying for terms in hierarchical categories using `hide_empty=true`, results have historically included parent terms which are themselves unattached to any objects (are "empty") but which have non-empty descendent terms. Because this process involves walking the descendant tree, we avoid it when we detect that the queried taxonomies are not hierarchical. (This behavior was introduced in [5525].) When the `taxonomy` parameter of `get_terms()` was made optional - see #35495, [36614] - it affected the mechanism for avoiding unneccessary tree walks, since there may not be any explicitly declared taxonomies to run through `is_taxonomy_hierarchical()`. As a result, term queries excluding `taxonomy` did not check descendants, and empty parents with non-empty children were not included in `hide_empty` results. We correct the behavior by crawling term descendants when the `taxonomy` argument is absent, which means that we're querying for terms in all taxonomies. Props smerriman. Fixes #37728. git-svn-id: https://develop.svn.wordpress.org/trunk@45888 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-term-query.php | 3 + tests/phpunit/tests/term/query.php | 79 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+) 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 ); + } }