Ensure that 'hierarchical' is respected in get_terms()
when multiple taxonomies are passed.
Previously, attempts to descend the family tree of each term were done using the first taxonomy in the `$taxonomies` array, with the result that terms not belonging to that taxonomy were not found and their children not properly parsed. We fix this bug by fetching each term's taxonomy with the SQL query, and then using that taxonomy to get the correct children for each term. Fixes #31118. git-svn-id: https://develop.svn.wordpress.org/trunk@31285 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
fed49d226c
commit
9d3a6394fb
@ -1887,10 +1887,10 @@ function get_terms( $taxonomies, $args = '' ) {
|
||||
break;
|
||||
case 'ids':
|
||||
case 'id=>parent':
|
||||
$selects = array( 't.term_id', 'tt.parent', 'tt.count' );
|
||||
$selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' );
|
||||
break;
|
||||
case 'names':
|
||||
$selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );
|
||||
$selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' );
|
||||
break;
|
||||
case 'count':
|
||||
$orderby = '';
|
||||
@ -1898,10 +1898,10 @@ function get_terms( $taxonomies, $args = '' ) {
|
||||
$selects = array( 'COUNT(*)' );
|
||||
break;
|
||||
case 'id=>name':
|
||||
$selects = array( 't.term_id', 't.name', 'tt.count' );
|
||||
$selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' );
|
||||
break;
|
||||
case 'id=>slug':
|
||||
$selects = array( 't.term_id', 't.slug', 'tt.count' );
|
||||
$selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' );
|
||||
break;
|
||||
}
|
||||
|
||||
@ -1974,14 +1974,15 @@ function get_terms( $taxonomies, $args = '' ) {
|
||||
_pad_term_counts( $terms, $_tax );
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure we show empty categories that have children.
|
||||
if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
|
||||
foreach ( $terms as $k => $term ) {
|
||||
if ( ! $term->count ) {
|
||||
$children = get_term_children( $term->term_id, reset( $taxonomies ) );
|
||||
$children = get_term_children( $term->term_id, $term->taxonomy );
|
||||
if ( is_array( $children ) ) {
|
||||
foreach ( $children as $child_id ) {
|
||||
$child = get_term( $child_id, reset( $taxonomies ) );
|
||||
$child = get_term( $child_id, $term->taxonomy );
|
||||
if ( $child->count ) {
|
||||
continue 2;
|
||||
}
|
||||
|
@ -1069,6 +1069,48 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
|
||||
$this->assertEqualSetsWithIndex( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 31118
|
||||
*/
|
||||
public function test_hierarchical_should_recurse_properly_for_all_taxonomies() {
|
||||
register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => true ) );
|
||||
register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
|
||||
|
||||
$t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
|
||||
$t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1', 'parent' => $t1 ) );
|
||||
$t3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1', 'parent' => $t2 ) );
|
||||
|
||||
$t4 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
|
||||
$t5 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t4 ) );
|
||||
$t6 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t5 ) );
|
||||
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
wp_set_object_terms( $p, $t3, 'wptests_tax1' );
|
||||
wp_set_object_terms( $p, $t6, 'wptests_tax2' );
|
||||
|
||||
$found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
|
||||
'hierarchical' => true,
|
||||
'hide_empty' => true,
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
/*
|
||||
* Should contain all terms, since they all have non-empty descendants.
|
||||
* To make the case clearer, test taxonomies separately.
|
||||
*/
|
||||
|
||||
// First taxonomy.
|
||||
$this->assertContains( $t1, $found );
|
||||
$this->assertContains( $t2, $found );
|
||||
$this->assertContains( $t3, $found );
|
||||
|
||||
// Second taxonomy.
|
||||
$this->assertContains( $t4, $found );
|
||||
$this->assertContains( $t5, $found );
|
||||
$this->assertContains( $t6, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 23261
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user