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.

git-svn-id: https://develop.svn.wordpress.org/trunk@36252 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-01-10 04:05:26 +00:00
parent e75a1f5ab3
commit 28818255c2
2 changed files with 20 additions and 1 deletions
+1 -1
View File
@@ -1604,7 +1604,7 @@ function get_terms( $taxonomies, $args = '' ) {
}
if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
$terms = array_slice( $terms, $offset, $number );
$terms = array_slice( $terms, $offset, $number, true );
}
wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
+19
View File
@@ -1665,6 +1665,25 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
}
/**
* @ticket 35382
*/
public function test_indexes_should_not_be_reset_when_number_of_matched_terms_is_greater_than_number() {
register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
$found = get_terms( 'wptests_tax', array(
'hide_empty' => false,
'fields' => 'id=>parent',
'number' => 2,
'orderby' => 'id',
'order' => 'ASC',
'hierarchical' => true,
) );
$this->assertSame( array( $terms[0], $terms[1] ), array_keys( $found ) );
}
protected function create_hierarchical_terms_and_posts() {
$terms = array();