diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index e5816580d1..0419e97424 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1629,7 +1629,14 @@ function get_terms( $taxonomies, $args = '' ) { $args['offset'] = absint( $args['offset'] ); // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. - if ( ! $single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ) { + $has_hierarchical_tax = false; + foreach ( $taxonomies as $_tax ) { + if ( is_taxonomy_hierarchical( $_tax ) ) { + $has_hierarchical_tax = true; + } + } + + if ( ! $has_hierarchical_tax ) { $args['hierarchical'] = false; $args['pad_counts'] = false; } @@ -1963,7 +1970,9 @@ function get_terms( $taxonomies, $args = '' ) { // Update term counts to include children. if ( $args['pad_counts'] && 'all' == $_fields ) { - _pad_term_counts( $terms, reset( $taxonomies ) ); + foreach ( $taxonomies as $_tax ) { + _pad_term_counts( $terms, $_tax ); + } } // Make sure we show empty categories that have children. if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) { diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 46db77e455..7abc13d622 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -1349,6 +1349,39 @@ class Tests_Term_getTerms extends WP_UnitTestCase { } } + /** + * @ticket 31118 + */ + public function test_pad_counts_should_work_when_first_taxonomy_is_nonhierarchical_and_second_taxonomy_is_hierarchical() { + register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => false ) ); + 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_tax2' ) ); + $t3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ) ); + + $posts = $this->factory->post->create_many( 3 ); + wp_set_object_terms( $posts[0], $t1, 'wptests_tax1' ); + wp_set_object_terms( $posts[1], $t2, 'wptests_tax2' ); + wp_set_object_terms( $posts[2], $t3, 'wptests_tax2' ); + + $found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array( + 'pad_counts' => true, + ) ); + + $this->assertEqualSets( array( $t1, $t2, $t3 ), wp_list_pluck( $found, 'term_id' ) ); + + foreach ( $found as $f ) { + if ( $t1 == $f->term_id ) { + $this->assertEquals( 1, $f->count ); + } elseif ( $t2 == $f->term_id ) { + $this->assertEquals( 2, $f->count ); + } else { + $this->assertEquals( 1, $f->count ); + } + } + } + protected function create_hierarchical_terms_and_posts() { $terms = array();