In get_terms(), don't set automatically hierarchical to false when parent => 0 is passed. The default value for parent is ''.

In `_get_term_children()`, don't skip a top-level term without first including its children in the returned term list. Ironically, the call to `_get_term_children()` in `get_terms()` has a comment stating `"Make sure we show empty categories that have children."`, but it didn't work if you were retrieving top-level categories only.

All unit tests pass. Added a unit test based on the use case described in this ticket.

Fixes #26903.



git-svn-id: https://develop.svn.wordpress.org/trunk@27108 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-02-06 17:44:50 +00:00
parent c2e018dd0d
commit 8dad11bfe2
2 changed files with 63 additions and 4 deletions

View File

@ -1279,7 +1279,7 @@ function get_terms($taxonomies, $args = '') {
$args['number'] = absint( $args['number'] );
$args['offset'] = absint( $args['offset'] );
if ( !$single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ||
'' !== $args['parent'] ) {
( '' !== $args['parent'] && 0 !== $args['parent'] ) ) {
$args['child_of'] = 0;
$args['hierarchical'] = false;
$args['pad_counts'] = false;
@ -3002,8 +3002,18 @@ function _get_term_children($term_id, $terms, $taxonomy) {
$use_id = true;
}
if ( $term->term_id == $term_id )
if ( $term->term_id == $term_id ) {
if ( isset( $has_children[$term_id] ) ) {
foreach ( $has_children[$term_id] as $t_id ) {
if ( $use_id ) {
$term_list[] = $t_id;
} else {
$term_list[] = get_term( $t_id, $taxonomy );
}
}
}
continue;
}
if ( $term->parent == $term_id ) {
if ( $use_id )
@ -3524,7 +3534,7 @@ function set_taxonomy_last_changed( $taxonomy ) {
/**
* Determine if a post's cache for the passed taxonomy
* is in sync.
*
*
* @since 3.9.0
*
* @param int $id

View File

@ -6,7 +6,7 @@
class Tests_Term_getTerms extends WP_UnitTestCase {
function setUp() {
parent::setUp();
_clean_term_filters();
wp_cache_delete( 'last_changed', 'terms' );
}
@ -224,4 +224,53 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$terms8 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => '.', 'fields' => 'ids' ) );
$this->assertEqualSets( array( $term_id1, $term_id2 ), $terms8 );
}
function test_get_terms_parent_zero() {
$tax = 'food';
register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
$cheese = $this->factory->term->create( array( 'name' => 'Cheese', 'taxonomy' => $tax ) );
$cheddar = $this->factory->term->create( array( 'name' => 'Cheddar', 'parent' => $cheese, 'taxonomy' => $tax ) );
$post_ids = $this->factory->post->create_many( 2 );
foreach ( $post_ids as $id ) {
wp_set_post_terms( $id, $cheddar, $tax );
}
$term = get_term( $cheddar, $tax );
$this->assertEquals( 2, $term->count );
$brie = $this->factory->term->create( array( 'name' => 'Brie', 'parent' => $cheese, 'taxonomy' => $tax ) );
$post_ids = $this->factory->post->create_many( 7 );
foreach ( $post_ids as $id ) {
wp_set_post_terms( $id, $brie, $tax );
}
$term = get_term( $brie, $tax );
$this->assertEquals( 7, $term->count );
$crackers = $this->factory->term->create( array( 'name' => 'Crackers', 'taxonomy' => $tax ) );
$butter = $this->factory->term->create( array( 'name' => 'Butter', 'parent' => $crackers, 'taxonomy' => $tax ) );
$post_ids = $this->factory->post->create_many( 1 );
foreach ( $post_ids as $id ) {
wp_set_post_terms( $id, $butter, $tax );
}
$term = get_term( $butter, $tax );
$this->assertEquals( 1, $term->count );
$multigrain = $this->factory->term->create( array( 'name' => 'Multigrain', 'parent' => $crackers, 'taxonomy' => $tax ) );
$post_ids = $this->factory->post->create_many( 3 );
foreach ( $post_ids as $id ) {
wp_set_post_terms( $id, $multigrain, $tax );
}
$term = get_term( $multigrain, $tax );
$this->assertEquals( 3, $term->count );
$fruit = $this->factory->term->create( array( 'name' => 'Fruit', 'taxonomy' => $tax ) );
$cranberries = $this->factory->term->create( array( 'name' => 'Cranberries', 'parent' => $fruit, 'taxonomy' => $tax ) );
$terms = get_terms( $tax, array( 'parent' => 0, 'cache_domain' => $tax ) );
$this->assertNotEmpty( $terms );
$this->assertEquals( wp_list_pluck( $terms, 'name' ), array( 'Cheese', 'Crackers' ) );
}
}