Free Wordpress Blog Setup

Display the Most Accurate Comment Count in WordPress

By Editorial Staff in Tutorials
Display the Most Accurate Comment Count in WordPress

When you are displaying comment count it is always good to display the accurate comment count. WordPress by default includes your trackbacks and pings into the total count which actually inflates the count. Specially some blogs that does not show trackbacks, or has them separated from the comments, you need to make sure that you are displaying the right count. In this article we will show you how you can display the correct comment count through a small snippet which will filter out trackbacks and pings and only display the actual comment count to your users.

Open your functions.php which is located in your template folder. And Paste the following code in there:

add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}

With this snippet, you can now show the most accurate comment count to your blog users.

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

3 Responses to “Display the Most Accurate Comment Count in WordPress”
  1. I was looking similar a wordpress plugin. This code very useful for me. Thank you.

  2. Very interesting article and very good blog. Very much interested to know)
    I will add this blog to RSS Reader

  3. Ezuca says:

    I have been searching the web for this solution. Plugins are available but I’m not into it. I tried your code and it works! Thanks a lot.

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar