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
This commit is contained in:
Boone Gorges 2019-08-23 16:04:07 +00:00
parent ae2b4f9add
commit 3e704473e7
2 changed files with 82 additions and 0 deletions

View File

@ -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 ) {

View File

@ -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 );
}
}