Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#35935 closed defect (bug) (fixed)

Correctly return zero terms when offset is greater than term count

Reported by: danielbachhuber's profile danielbachhuber Owned by: boonebgorges's profile boonebgorges
Milestone: 4.5 Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch has-unit-tests needs-testing
Focuses: Cc:

Attachments (2)

35935.1.diff (1.9 KB) - added by danielbachhuber 7 years ago.
35935.diff (3.3 KB) - added by boonebgorges 7 years ago.

Download all attachments as: .zip

Change History (7)

This ticket was mentioned in Slack in #core by danielbachhuber. View the logs.


7 years ago

#2 @boonebgorges
7 years ago

  • Owner set to boonebgorges
  • Status changed from new to reviewing

@boonebgorges
7 years ago

#3 @boonebgorges
7 years ago

  • Keywords needs-testing added

Thanks for the ticket and the patch. I've confirmed the issue.

Your fix doesn't go far enough. Your tests are using very large numbers for 'offset' and 'number', which is masking another bug: get_terms() is not properly trimming the array when (number + offset) > count( $terms ) > number. The fix appears to be to skip the count( $terms ) > $number check, and run all results that make it this far through array_slice(). See 35935.diff. Could you double-check my logic?

This collection of bugs exists since [10416], when the fix for hierarchical limits in get_terms() was introduced.

#4 @danielbachhuber
7 years ago

Your patch doesn't have glaring issues I can see, and works against the (more limited) tests I wrote for WP-API

#5 @boonebgorges
7 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 36691:

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.

Note: See TracTickets for help on using tickets.