From 28818255c2d58c93c37acf475f9c9f4bb0a8b532 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 10 Jan 2016 04:05:26 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 2 +- tests/phpunit/tests/term/getTerms.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index cd5b92a345..5f5127ac10 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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 ); diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index bcd348586b..ec261f8ffc 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -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();