[wp-trac] [WordPress Trac] #43995: wp_dropdown_categories(array( 'pad_counts' => true ) ) doesn't correctly pad counts on post_types with custom stati

WordPress Trac noreply at wordpress.org
Mon May 7 17:54:22 UTC 2018


#43995: wp_dropdown_categories(array( 'pad_counts' => true ) ) doesn't correctly
pad counts on post_types with custom stati
--------------------------+------------------------------
 Reporter:  pbiron        |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Taxonomy      |     Version:  trunk
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by pbiron):

 The problem is in
 [[https://developer.wordpress.org/reference/functions/_pad_term_counts/|_pad_term_counts()]]
 ([[https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/taxonomy.php#L3537|/wp-includes/taxonomy.php#L3537]]):

 {{{
 #!php
 $results = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM
 $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE
 term_taxonomy_id IN (" . implode( ',', array_keys( $term_ids ) ) . ") AND
 post_type IN ('" . implode( "', '", $object_types ) . "') AND post_status
 = 'publish'" );
 }}}

 which hardcodes `...AND post_status = 'publish'` into the query.

 My initial stab at a fix is to replace
 [[https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/taxonomy.php#L3537|/wp-includes/taxonomy.php#L3537]] with

 {{{
 #!php
 $post_stati = apply_filters( 'pad_term_counts_post_stati', array(
 'publish' ), $taxonomy, $object_types );
 $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM
 $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE
 term_taxonomy_id IN (" . implode(',', array_keys($term_ids)) . ") AND
 post_type IN ('" . implode("', '", $object_types) . "') AND post_status IN
 ('" . implode("', '", $post_stati) . "')");
 }}}

 which would allow me to write:

 {{{
 #!php
 add_filter( 'pad_term_counts_post_stati', 'my_pad_term_counts_post_stati'
 ), 10, 3 );
 function my_pad_term_counts_post_stati( $post_stati, $taxonomy,
 $object_types ) {
         if ( in_array( 'member', $object_types ) && in_array( 'membership-
 status', (array) $taxonomy ) ) {
                 $post_stati = array( 'subscribed', 'unsubscribed' );
         }

         return $post_stati;
 }
 }}}

 The above would certainly solve **my** immediate need, but there might be
 other more generalized use cases that would require some other solution.

 If others agree that the problem is real and that __some__ fix is desired,
 I'll produce a patch and some unit tests that cover **my** immediate need.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/43995#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list