<?php

function custom_tweak_wp_count_comments( $post_id = 0 ) {
		global $wpdb;

		$args = func_get_args();
		$post_id = $args[1];

		$post_id = (int) $post_id;
		
		$count = wp_cache_get("comments-{$post_id}", 'counts');

		if ( false !== $count )
				return $count;

		
		$where = 'WHERE';
		if ( $post_id > 0 )
				$where .= $wpdb->prepare( " comment_post_ID = %d AND", $post_id );

		$query = "(SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = 'trash') UNION ALL
(SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = 'spam') UNION ALL
(SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = '0') UNION ALL
(SELECT COUNT( comment_ID ) FROM {$wpdb->comments} {$where} comment_approved = 'post-trash') UNION ALL
(SELECT COUNT( comment_ID ) FROM {$wpdb->comments});";

		$counts = $wpdb->get_col($query);

		$trash = $counts[0];
		$spam = $counts[1];
		$unapproved = $counts[2];
		$post_trash = $counts[3];
		$all = $counts[4];

		$approved = $all - $unapproved - $spam - $trash - $post_trash;

		$stats = array(
			'approved'						=> $approved,
			'moderated'						=> $unapproved,
			'spam'								=> $spam,
			'trash'								=> $trash,
			'post-trashed'				=> $post_trash,
			'total_comments'			=> $spam + $approved + $unapproved,
			'all'									=> $approved + $unapproved,
		);

		$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 );

?>
