WordPress.org

Make WordPress Core

Changeset 31284


Ignore:
Timestamp:
01/26/2015 06:08:59 PM (6 years ago)
Author:
boonebgorges
Message:

Ensure that 'pad_counts' is not discarded when the first of multiple taxonomies passed to get_terms() is non-hierarchical.

See #31118.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r31276 r31284  
    16301630
    16311631    // Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
    1632     if ( ! $single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ) {
     1632    $has_hierarchical_tax = false;
     1633    foreach ( $taxonomies as $_tax ) {
     1634        if ( is_taxonomy_hierarchical( $_tax ) ) {
     1635            $has_hierarchical_tax = true;
     1636        }
     1637    }
     1638
     1639    if ( ! $has_hierarchical_tax ) {
    16331640        $args['hierarchical'] = false;
    16341641        $args['pad_counts'] = false;
     
    19641971    // Update term counts to include children.
    19651972    if ( $args['pad_counts'] && 'all' == $_fields ) {
    1966         _pad_term_counts( $terms, reset( $taxonomies ) );
     1973        foreach ( $taxonomies as $_tax ) {
     1974            _pad_term_counts( $terms, $_tax );
     1975        }
    19671976    }
    19681977    // Make sure we show empty categories that have children.
  • trunk/tests/phpunit/tests/term/getTerms.php

    r31280 r31284  
    13501350    }
    13511351
     1352    /**
     1353     * @ticket 31118
     1354     */
     1355    public function test_pad_counts_should_work_when_first_taxonomy_is_nonhierarchical_and_second_taxonomy_is_hierarchical() {
     1356        register_taxonomy( 'wptests_tax1', 'post', array( 'hierarchical' => false ) );
     1357        register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true ) );
     1358
     1359        $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     1360        $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     1361        $t3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'parent' => $t2 ) );
     1362
     1363        $posts = $this->factory->post->create_many( 3 );
     1364        wp_set_object_terms( $posts[0], $t1, 'wptests_tax1' );
     1365        wp_set_object_terms( $posts[1], $t2, 'wptests_tax2' );
     1366        wp_set_object_terms( $posts[2], $t3, 'wptests_tax2' );
     1367
     1368        $found = get_terms( array( 'wptests_tax1', 'wptests_tax2' ), array(
     1369            'pad_counts' => true,
     1370        ) );
     1371
     1372        $this->assertEqualSets( array( $t1, $t2, $t3 ), wp_list_pluck( $found, 'term_id' ) );
     1373
     1374        foreach ( $found as $f ) {
     1375            if ( $t1 == $f->term_id ) {
     1376                $this->assertEquals( 1, $f->count );
     1377            } elseif ( $t2 == $f->term_id ) {
     1378                $this->assertEquals( 2, $f->count );
     1379            } else {
     1380                $this->assertEquals( 1, $f->count );
     1381            }
     1382        }
     1383    }
     1384
    13521385    protected function create_hierarchical_terms_and_posts() {
    13531386        $terms = array();
Note: See TracChangeset for help on using the changeset viewer.