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.

git-svn-id: https://develop.svn.wordpress.org/trunk@36691 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-02-24 19:12:45 +00:00
parent 12645e6f75
commit 036bac45bc
2 changed files with 81 additions and 2 deletions
+7 -2
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 );