0 ) $where .= $wpdb->prepare( " comment_post_ID = %d AND", $post_id ); $unapproved = $wpdb->get_var( "SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = '0' " ); $spam = $wpdb->get_var( "SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = 'spam' " ); $trash = $wpdb->get_var( "SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = 'trash' " ); $post_trash = $wpdb->get_var( "SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = 'post-trash' " ); /*if ( $post_id > 0 ) { $approved = $wpdb->get_var( "SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = '1' " ); }*/ $approved = $wpdb->get_var( "SELECT COUNT( comment_ID ) FROM {$wpdb->comments} " ); $approved = $approved - $unapproved - $spam - $trash - $post_trash; $count = array(); $count[] = array( 'comment_approved' => '0', 'num_comments' => $unapproved ); $count[] = array( 'comment_approved' => '1', 'num_comments' => $approved ); $count[] = array( 'comment_approved' => 'spam', 'num_comments' => $spam ); $count[] = array( 'comment_approved' => 'trash', 'num_comments' => $trash ); $count[] = array( 'comment_approved' => 'post-trash', 'num_comments' => $post_trash ); /* Here is the standard query for reference */ //$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A ); $total = 0; $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed'); foreach ( (array) $count as $row ) { // Don't count post-trashed toward totals if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) $total += $row['num_comments']; if ( isset( $approved[$row['comment_approved']] ) ) $stats[$approved[$row['comment_approved']]] = $row['num_comments']; } $stats['total_comments'] = $total; foreach ( $approved as $key ) { if ( empty($stats[$key]) ) $stats[$key] = 0; } $stats = (object) $stats; wp_cache_set("comments-{$post_id}", $stats, 'counts'); return $stats; } add_filter('wp_count_comments', 'custom_tweak_wp_count_comments', 10, 2 ); ?>