WordPress.org

Make WordPress Core

Changeset 36252


Ignore:
Timestamp:
01/10/2016 04:05:26 AM (5 years ago)
Author:
boonebgorges
Message:

Don't reset index keys when trimming results of term queries.

array_slice() must be told to preserve keys when the query results exceed the
limit specified the 'number' parameter, so that id=>parent and other
id-indexed return value formats don't get mangled.

Props fantasyworld, wpdelighter.
Fixes #35382.

Location:
trunk
Files:
2 edited

Legend:

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

    r36244 r36252  
    16051605
    16061606    if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
    1607         $terms = array_slice( $terms, $offset, $number );
     1607        $terms = array_slice( $terms, $offset, $number, true );
    16081608    }
    16091609
  • trunk/tests/phpunit/tests/term/getTerms.php

    r36003 r36252  
    16661666    }
    16671667
     1668    /**
     1669     * @ticket 35382
     1670     */
     1671    public function test_indexes_should_not_be_reset_when_number_of_matched_terms_is_greater_than_number() {
     1672        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     1673        $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     1674
     1675        $found = get_terms( 'wptests_tax', array(
     1676            'hide_empty' => false,
     1677            'fields' => 'id=>parent',
     1678            'number' => 2,
     1679            'orderby' => 'id',
     1680            'order' => 'ASC',
     1681            'hierarchical' => true,
     1682        ) );
     1683
     1684        $this->assertSame( array( $terms[0], $terms[1] ), array_keys( $found ) );
     1685    }
     1686
    16681687    protected function create_hierarchical_terms_and_posts() {
    16691688        $terms = array();
Note: See TracChangeset for help on using the changeset viewer.