Improve 'offset' calculation when querying for hierarchical terms.

When querying for terms in hierarchical taxonomies, `get_terms()` initially
queries for all matching terms, and then trims the located results based on the
`$number` and `$offset` arguments passed to the function. See #8832. However,
a flaw in the original logic meant that results were failing to be trimmed
properly in cases where `$offset` exceeds the total number of matching terms;
in these cases, we should force an empty array.

Props danielbachhuber.
Fixes #35935.
Built from https://develop.svn.wordpress.org/trunk@36691


git-svn-id: http://core.svn.wordpress.org/trunk@36658 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2016-02-24 19:13:26 +00:00
parent 6f2fbfb28b
commit b694240cd9
2 changed files with 8 additions and 3 deletions

View File

@ -1719,8 +1719,13 @@ function get_terms( $args = array(), $deprecated = '' ) {
$terms = $_terms;
}
if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
$terms = array_slice( $terms, $offset, $number, true );
// Hierarchical queries are not limited, so 'offset' and 'number' must be handled now.
if ( $hierarchical && $number && is_array( $terms ) ) {
if ( $offset >= count( $terms ) ) {
$terms = array();
} else {
$terms = array_slice( $terms, $offset, $number, true );
}
}
wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.5-alpha-36690';
$wp_version = '4.5-alpha-36691';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.