Make WordPress Core

Opened 15 years ago

Closed 15 years ago

Last modified 8 years ago

#8832 closed defect (bug) (fixed)

function get_categories() doesn't work properly when using child_of and number parameters

Reported by: jidm's profile jidm Owned by: ryan's profile ryan
Milestone: 2.7.1 Priority: normal
Severity: normal Version: 2.7
Component: Taxonomy Keywords: has-patch get_terms number child_of
Focuses: Cc:


The function get_categories() does not behave as expected when combining parameters "child_of" and "number".

Instead of showing X children of category Y, they are doing the following logic:
Get X categories (children or not). Then look in this result for children of category Y and show them. So the total result can be 5 categories, 3 categories, or even none.

Attachments (1)

number_get_terms_fix.diff (1.8 KB) - added by filosofo 15 years ago.

Download all attachments as: .zip

Change History (8)

#1 @filosofo
15 years ago

  • Keywords has-patch get_terms number child_of added

Patch ignores number for the initial query whenever descending the taxonomy family tree is called for (when child_of is non-zero, parent is an integer, or hierarchical is true. It does a final terms count limiting at the end.

#2 @filosofo
15 years ago

To explain a little better:

What happens currently is that get_terms sets its initial database query limit to the "number" parameter, if supplied, and queries the database. It then goes through the returned terms and prunes out ones that aren't children of X term, or are empty, etc., depending on the other parameters.

So the number of terms returned by get_terms ultimately does not reflect the number of terms matching the parameters limited by the "number" parameter; rather, it reflects the number of terms returned by the initial query, some of which might not match all the parameters and will therefore be pruned out later.

My patch ignores the "number" parameter for the initial database query in those cases in which it's possible that some terms will later be pruned: e.g. the ones I mention above: when getting child_of, parent, etc. It then applies it to the array of terms before get_terms returns it.

#3 @jidm
15 years ago

Thank you filosofo!

This worked for me. The bug can be closed now.

#4 @ryan
15 years ago

  • Component changed from General to Taxonomy
  • Owner changed from anonymous to ryan

#5 @ryan
15 years ago

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

(In [10416]) Don't limit query when descending hierarchy. Props filosofo. fixes #8832

#6 @ryan
15 years ago

(In [10417]) Don't limit query when descending hierarchy. Props filosofo. fixes #8832 for 2.7

#7 @boonebgorges
8 years ago

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.