diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 464a00ca3f..b2336c8a59 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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 diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 0db732bf0a..c232d4fd93 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -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' ) ); + } }