The last couple of days I posted tutorials on how to separate trackbacks from comments and how to display them in different tabs using jQuery. In the last tutorial about the jQuery tabs I also posted a solution on how to correct the comments count by excluding trackbacks and pingbacks. However, on my own theme I wanted to take it a step further and show separate counts for trackbacks and pingbacks as well. I had a tough time trying to find a proper solution for this though, but I finally found something which deserves mentioning here.
Installation
There is a WordPress plugin that contains two functions that return and echo the number of pingbacks, trackbacks, combined pings or regular comments for a WordPress entry. You can either install this plugin or add these two functions to your functions.php theme file.
If you choose to add these two functions to your theme by hand (and not by installing the plugin) continue reading. If you followed my previous tutorial or any other tutorial to correct the comment count you can replace this function with the two new functions of this plugin in functions.php.
Locate this code if available in your functions.php:
<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
global $id;
$comments_by_type = &separate_comments(get_comments('post_id=' . $id));
return count($comments_by_type['comment']);
}
?>
If actually there replace this with the code below. If not there just add the next piece of code at the bottom of functions.php.
<?php
function comment_type_count($type = 'all', $post_id = 0) { echo get_comment_type_count($type, $post_id); }
function update_comment_type_cache(&$queried_posts) {
global $cjd_comment_count_cache, $wpdb;
if ( !$queried_posts )
return $queried_posts;
foreach ( (array) $queried_posts as $post )
if ( !isset($cjd_comment_count_cache[$post->ID]) )
$post_id_list[] = $post->ID;
if ( $post_id_list ) {
$post_id_list = implode(',', $post_id_list);
foreach ( array('', 'pingback', 'trackback') as $type ) {
$counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
FROM $wpdb->posts
LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID AND comment_approved = '1' AND comment_type='$type' )
WHERE post_status = 'publish' AND ID IN ($post_id_list)
GROUP BY ID");
if ( $counts ) {
if ( '' == $type )
$type = 'comment';
foreach ( $counts as $count )
$cjd_comment_count_cache[$count->ID][$type] = $count->ccount;
}
}
}
return $queried_posts;
}
add_filter('the_posts', 'update_comment_type_cache');
?>
Usage
These usage instructions are taken from the plugin author’s website. You can use the extra parameters in your theme files to show the counts for comments, trackbacks and pingbacks.
There are two functions you can use: get_comment_type_count() (which returns the value) and comment_type_count() (which echoes the value). Both functions operate identically, and take 2 optional parameters. The first parameter is the type of comment you would like returned (defaults to ‘all’). Your options are trackback, pingback, ping, and comment. For example, pretend that a post has 1 pingback, 2 trackbacks, and 4 regular comments.
<?php
comment_type_count('pingback'); // prints "1"
comment_type_count('trackback'); // prints "2"
comment_type_count('ping'); // prints "3" (1 pingback + 2 trackbacks)
comment_type_count('comment'); // prints "4"
comment_type_count(); // prints "7" (1 pingback + 2 trackbacks + 4 comments)
?>
The second parameter, which is also optional, is a post ID. By default, the function just gives you the number for the current post being processed in “the loop.” By passing a specific post ID, you can get the count for any post ID you wish.
<?php
comment_type_count('pingback', 534); // prints number of pingbacks on entry 534
?>
I think it’s strange that WordPress can not do this by default. Perhaps this will be a new feature soon but for the time being this will work fine.


That is so wonderful. The codes are very easy to implement. No problem at all. Thanks for making my work easy. It’s now giving the correct count.