WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 months ago

#16863 closed defect (bug) (worksforme)

get_terms hierarchical/hide_empty incompatible with include/exclude args

Reported by: martinshopland Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Taxonomy Keywords: needs-unit-tests needs-refresh
Focuses: Cc:

Description

$terms = get_terms('taxName', array(
    'hierarchical' => true,
    'hide_empty' => true,
    'include' => array(1, 2, 3)
));

$terms will not contain a meaningful result set. In order for get_terms to process 'hierarchical' correctly it must recieve in 'include' a full list of term ids for all child terms that may have count > 0, this obviously negates the point of 'include' here.
'include/exclude' is processed at query level, 'hierarchical' is processed post query and expects the results of the query to be unfiltered.
The following patch seems to fix the issue in my application, I think this approach would need to take in 'child_of' also, which I haven't done here.

diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php
index 89532dc..2f51bea 100644
--- a/wp-includes/taxonomy.php
+++ b/wp-includes/taxonomy.php
@@ -1337,9 +1337,13 @@ function &get_terms($taxonomies, $args = '') {
 
 	// Make sure we show empty categories that have children.
 	if ( $hierarchical && $hide_empty && is_array($terms) ) {
+
+        $taxTerms = !(empty($include) && empty($exclude) && empty($exclude_tree)) ?
+            get_terms($taxonomies) : $terms;
+
 		foreach ( $terms as $k => $term ) {
 			if ( ! $term->count ) {
-				$children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
+				$children = _get_term_children($term->term_id, $taxTerms, $taxonomies[0]);
 				if ( is_array($children) )
 					foreach ( $children as $child )
 						if ( $child->count )

Attachments (2)

16863.patch (778 bytes) - added by SergeyBiryukov 4 years ago.
Actual patch
16863-test.diff (1.2 KB) - added by boonebgorges 4 months ago.

Download all attachments as: .zip

Change History (10)

@SergeyBiryukov4 years ago

Actual patch

comment:2 @SergeyBiryukov3 years ago

Related: ticket:13176:13

Last edited 3 years ago by SergeyBiryukov (previous) (diff)

comment:3 @nacin16 months ago

  • Component changed from General to Taxonomy

comment:4 @wonderboymusic13 months ago

  • Milestone changed from Awaiting Review to 4.0

comment:5 @wonderboymusic13 months ago

  • Keywords needs-unit-tests added

comment:6 @SergeyBiryukov10 months ago

  • Milestone changed from 4.0 to Future Release

comment:7 @MikeHansenMe7 months ago

  • Keywords needs-refresh added; has-patch removed

@boonebgorges4 months ago

comment:8 @boonebgorges4 months ago

  • Milestone Future Release deleted
  • Resolution set to worksforme
  • Status changed from new to closed

It appears that this is no longer a problem. See 16863-test.diff. I believe the problem was probably fixed by [27458], because the switch from _get_term_children() to get_term_children() means that the term hierarchy is now calculated independently of the 'hide_empty' query.

Note: See TracTickets for help on using tickets.