Skip to content
Permalink
Browse files

Taxonomy: Ensure consistency of `hide_empty` in term queries when `ta…

…xonomy` 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
  • Loading branch information...
boonebgorges committed Aug 23, 2019
1 parent ae2b4f9 commit 3e704473e709479f680f3e93747cbc9a17203663
Showing with 82 additions and 0 deletions.
  1. +3 −0 src/wp-includes/class-wp-term-query.php
  2. +79 −0 tests/phpunit/tests/term/query.php
@@ -326,6 +326,9 @@ public function get_terms() {
$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 ) {
@@ -767,4 +767,83 @@ public function test_terms_pre_query_filter_should_bypass_database_query() {
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 );
}
}

0 comments on commit 3e70447

Please sign in to comment.
You can’t perform that action at this time.