Make WordPress Core

Opened 14 years ago

Closed 12 years ago

Last modified 12 years ago

#13176 closed defect (bug) (worksforme)

wp_list_categories pad_counts shows zero for parent term with custom hierarchical taxonomy

Reported by: michaelh's profile michaelh Owned by: filosofo's profile filosofo
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Taxonomy Keywords: reporter-feedback
Focuses: Cc:

Description

When asked to "pad_counts", this version of wp_list_categories returns zero for parent terms when using a custom hierarchical taxonomy:

<?php 
$orderby = 'name'; 
$show_count = 1; 
$pad_counts = 1; 
$hierarchical = 1; 
$taxonomy = 'genre';
$title = '';
$args = array(
  'orderby' => $orderby,
  'show_count' => $show_count,
  'pad_counts' => $pad_counts,
  'hierarchical' => $hierarchical,
  'taxonomy' => $taxonomy,
  'title_li' => $title
);
?>
<ul>
<?php 
wp_list_categories($args);
?>
</ul>

With $taxonomy = 'category', the pad_counts are correct.

Change History (15)

#1 @kevinB
14 years ago

  • Cc kevinB added

#2 @nacin
13 years ago

  • Milestone set to Awaiting Review

So something is wrong in _pad_term_counts()?

#3 @westi
13 years ago

  • Keywords reporter-feedback added
  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

We need more information on how to reproduce this code looks ok to me.

#4 follow-ups: @valendesigns
13 years ago

  • Keywords needs-patch added
  • Resolution worksforme deleted
  • Status changed from closed to reopened
  • Version changed from 3.0 to 3.2.1

I'm having a similar issue. However, it's not limited to custom taxonomies. Basically, when you select a child category in the post or custom post type and don't select its parent the pad count doesn't work correctly.

As well, if you look at the category list in the WordPress admin the parent will show a 0 count even though it has posts in the children categories.

Is that enough information to reproduce the issue?

#5 @ocean90
13 years ago

  • Version changed from 3.2.1 to 3.0

The version field is used to track when a bug was first reported.

#6 in reply to: ↑ 4 ; follow-up: @SergeyBiryukov
13 years ago

Replying to valendesigns:

Is that enough information to reproduce the issue?

Not exactly. I've just tried to reproduce with this structure:

Category 1 (0 posts)
— Category 2 (3 posts)
— — Category 3 (2 posts)

Calling wp_list_categories('show_count=1&pad_counts=1'); results in:

Category 1 (5)
    Category 2 (5)
        Category 3 (2)

Which seems OK.

I've also tried to reproduce the original bug report from ticket description, and the code seems to work as expected too.

#7 in reply to: ↑ 4 @SergeyBiryukov
13 years ago

Replying to valendesigns:

As well, if you look at the category list in the WordPress admin the parent will show a 0 count even though it has posts in the children categories.

The category list in the admin doesn't use pad_counts.

#8 in reply to: ↑ 6 @valendesigns
13 years ago

I'm also using a list of child category ID's in the "include" option. The way I'm building the list of categories is to grab all the ID's that have 0 as there parent and put them into an array with get_categories() and then the rest go into the child array. It doesn't show all the parents because it thinks they are empty and doesn't pad their counts. Here's the code.

if ( ! function_exists( 'premium_get_categories' ) ) {
  function premium_get_categories() {
    $return     = '';
    $parents    = array();
    $children   = array();
    $args = array(
      'orderby' => 'id',
      'order'   => 'ASC'
    );
    $categories = get_categories( $args );
    
    foreach( $categories as $category )
      if ( $category->category_parent == 0 )
        $parents[] = $category->term_id;
      else
        $children[] = $category->term_id;
    
    if ( count( $parents ) > 0 )
      $return.= wp_list_categories( 'include=' . implode( ',', $parents ) . '&title_li=<h4>' . __( 'Topics:' ) . '</h4>&show_count=1&pad_counts=1&echo=0' );
    
    if ( count( $children ) > 0 )
      $return.= wp_list_categories( 'include=' . implode( ',', $children ) . '&title_li=<h4>' . __( 'Category:' ) . '</h4>&show_count=1&pad_counts=1&echo=0' );
    
    if ( $return !== '' )
      return '<ul id="sidebar-categories">' . $return . '</ul>';   
  }
}

Replying to SergeyBiryukov:

Replying to valendesigns:

Is that enough information to reproduce the issue?

Not exactly. I've just tried to reproduce with this structure:

Category 1 (0 posts)
— Category 2 (3 posts)
— — Category 3 (2 posts)

Calling wp_list_categories('show_count=1&pad_counts=1'); results in:

Category 1 (5)
    Category 2 (5)
        Category 3 (2)

Which seems OK.

I've also tried to reproduce the original bug report from ticket description, and the code seems to work as expected too.

#9 @witthansen
13 years ago

  • Cc christopher.witt.hansen@… added

#10 @SergeyBiryukov
13 years ago

  • Milestone set to Awaiting Review

#11 @wonderboymusic
12 years ago

  • Keywords needs-patch removed
  • Resolution set to worksforme
  • Status changed from reopened to closed

I cannot reproduce this - I added 3 terms (term_id = 10, 11, 12) 12 is child of 11 is child of 10:

add_action( 'init', function () {
	register_taxonomy( 'custom_thing', 'post', array( 'hierarchical' => true, 'pad_counts' => 1, 'labels' => array( 'name' => 'Custom Thing' ) ) );
	wp_list_categories( 'show_count=1&pad_counts=1&taxonomy=custom_thing&include=10,11,12' );
	exit();
} );

Works just fine

#12 @helenyhou
12 years ago

  • Milestone Awaiting Review deleted

#13 @SergeyBiryukov
12 years ago

I can reproduce valendesigns' example from comment:8.

  1. Created the structure:
    Cat 1 (0 posts, ID 66)
    — sub cat 1 (1 post, ID 67)
    — — sub sub cat 1 (1 post, ID 76)
    
  2. wp_list_categories( 'show_count=1&pad_counts=1' ) displays:
    Cat 1 (2)
        sub cat 1 (2)
            sub sub cat 1 (1)
    
  3. wp_list_categories('include=66&show_count=1&pad_counts=1&hide_empty=0'):
    Cat 1 (0)
    

When include is specified, pad_counts only applies to the categories that are explicitly included.

Not sure if that's a bug or expected behaviour though.

#14 @SergeyBiryukov
12 years ago

  • Resolution changed from worksforme to fixed

Related: #16863

#15 @SergeyBiryukov
12 years ago

  • Resolution changed from fixed to worksforme

Hmm, didn't mean to change the resolution.

Note: See TracTickets for help on using tickets.