Make WordPress Core

Opened 13 years ago

Closed 11 years ago

Last modified 11 years ago

#17365 closed defect (bug) (duplicate)

get_terms doesn't return terms with no posts if you specify a parent

Reported by: mark8barnes's profile mark8barnes Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1.2
Component: Taxonomy Keywords:
Focuses: Cc:

Description

get_terms doesn't return terms with no posts if you specify a parent (see also #15626 for a related bug). Let's imagine the following term structure:

---parent_term (0 count)
    |---child_term (10 count)

Calling get_terms('category', array('parent' => 0)) should return both terms according to the documentation, but it doesn't. The documentation says that hierarchical defaults to true, which should "include terms that have non-empty descendants". It's broken because the following code runs if a parent argument is present (lines 1139-3 of taxonomies.php).

if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) ||
	'' !== $args['parent'] ) {
$args['child_of'] = 0;
$args['hierarchical'] = false;
$args['pad_counts'] = false;

But nothing in the parent section of the documentation suggests that adding a parent will change the other defaults. The consequence of the above code comes in lines 1266-7:

if ( $hide_empty && !$hierarchical )
	$where .= ' AND tt.count > 0';

which means the parent_term is not being returned because it has a count of zero, and I can't walk down the tree to get the other terms.

At the very least, set this paramenter as default in $defaults, before $args = wp_parse_args( $args, $defaults ) (rather than in $args afterwards) which would at least allow us to override this behaviour through explicit $atts. But it would be more logical simply to remove the test for '' !== args['parent'] altogther, inline with the existing documentation.

Change History (4)

#1 follow-up: @c3mdigital
11 years ago

  • Keywords close added
  • Resolution set to worksforme
  • Status changed from new to closed

mark8barnes,

In your example you are setting 'parent' => 0 When you set parent to 0 no child terms should be returned. If you set parent to the parent terms term_id even though it is empty you will still get the child terms.

I'm closing. If you still think this is a bug please re open with additional information to support.

#2 @helen
11 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted

#3 in reply to: ↑ 1 @mrwweb
11 years ago

Replying to c3mdigital:

I'm closing. If you still think this is a bug please re open with additional information to support.

Reopening this ticket as #26903.

c3mdigital, regarding this:

When you set parent to 0 no child terms should be returned.

The problem is not that child terms aren't returned but that the parent terms themselves aren't returned either.

#4 @SergeyBiryukov
11 years ago

  • Resolution changed from worksforme to duplicate

Duplicate of #26903.

Note: See TracTickets for help on using tickets.