WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 21 months ago

#37728 new defect (bug)

hide_empty doesn't work correctly in get_terms when no taxonomy specified

Reported by: smerriman Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.5
Component: Taxonomy Keywords: has-patch needs-testing needs-unit-tests
Focuses: Cc:

Description

In #35495, the taxonomy argument to get_terms was made optional. Calling get_terms without specifying a taxonomy will return terms from all taxonomies.

However, if taxonomy isn't provided, hide_empty (when true, as per default) does not work correctly. Parent terms which are empty themselves but have nonempty child terms are not returned.

You can replicate this by creating an empty category with non-empty child categories. Using:

get_terms() - won't include the category get_terms(array('taxonomy'=>'category')) - will include the category get_terms(array('taxonomy'=>array('category','post_tag'))) - will include the category

This is caused by the get_terms function in WP_Term_Query. $has_hierarchical_tax gets set to true if a single hierarchical taxonomy is passed, or multiple taxonomies with at least one hierarchical. However, if no taxonomies are passed, this never gets set to true.

In this case it should be set to true, since it will at minimum include the in-built category taxonomy.

Attachments (1)

37728.patch (386 bytes) - added by smerriman 21 months ago.
set has_hierarchical_tax to true when querying all taxonomies

Download all attachments as: .zip

Change History (2)

@smerriman
21 months ago

set has_hierarchical_tax to true when querying all taxonomies

#1 @swissspidy
21 months ago

  • Keywords has-patch needs-testing needs-unit-tests added
Note: See TracTickets for help on using tickets.