WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#13176 closed defect (bug) (worksforme)

wp_list_categories pad_counts shows zero for parent term with custom hierarchical taxonomy

Reported by: michaelh Owned by: 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)

comment:1 @kevinB5 years ago

  • Cc kevinB added

comment:2 @nacin5 years ago

  • Milestone set to Awaiting Review

So something is wrong in _pad_term_counts()?

comment:3 @westi5 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.

comment:4 follow-ups: @valendesigns4 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?

comment:5 @ocean904 years ago

  • Version changed from 3.2.1 to 3.0

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

comment:6 in reply to: ↑ 4 ; follow-up: @SergeyBiryukov4 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.

comment:7 in reply to: ↑ 4 @SergeyBiryukov4 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.

comment:8 in reply to: ↑ 6 @valendesigns4 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.

comment:9 @witthansen4 years ago

  • Cc christopher.witt.hansen@… added

comment:10 @SergeyBiryukov4 years ago

  • Milestone set to Awaiting Review

comment:11 @wonderboymusic3 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

comment:12 @helenyhou3 years ago

  • Milestone Awaiting Review deleted

comment:13 @SergeyBiryukov3 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.

comment:14 @SergeyBiryukov3 years ago

  • Resolution changed from worksforme to fixed

Related: #16863

comment:15 @SergeyBiryukov3 years ago

  • Resolution changed from fixed to worksforme

Hmm, didn't mean to change the resolution.

Note: See TracTickets for help on using tickets.