Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Display the Most Accurate Comment Count in WordPress

Do you want to display the most accurate comment count in WordPress?

Your WordPress blog may have comments that are pending moderation, spam, trackback and pings. By default, there is no easy way to display the exact comment count on your WordPress site.

In this article, we will show you how to easily display the most accurate comment count in WordPress.

Display accurate comment count in WordPress

Method 1. Display Comment Count in WordPress Using Plugin

If you are not comfortable with adding code to your WordPress site then this method provides an easy way to display the total comment count on your site including pingbacks and trackbacks.

First, you need to install and activate the Simple Blog Stats plugin. For more details, see our step by step guide on how to install a WordPress plugin.

The plugin provides a bunch of shortcodes that you can use anywhere on your WordPress website to display different stats about your site. For instance, you can display the total number of posts, users, or comments.

You can view all the shortcodes by visiting Settings » Simple Blog Stats page.

Simple blog stats shortcodes

To display the total count of all approved comments on your website you can use the following shortcode:

[sbs_approved]

After that, you can view your website and you’ll be able to see the total comment count of all approved comments on your website.

Comment count via plugin

The problem with this method is that it does include all the approved pings and trackbacks in your WordPress comment count.

These days, most websites turn off trackbacks and pings due to block spam comments in WordPress. To learn more see our complete guide on WordPress trackbacks and pings and how to disable them.

However, if you want to keep the trackbacks and pings enabled on your website, then you may want to use the next method which wouldn’t include them in your comment count.

Method 2. Manually Display Most Accurate Comment Count in WordPress

This method requires you to add custom code to your WordPress website. If you haven’t done this before, then take a look at our guide on how to copy and paste code snippets in WordPress.

First, you need to paste the following code to your WordPress site:

function wpbeginner_comment_count() { 

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

$actual_comment_count = get_comments_number(); 

return $actual_comment_count;

}

add_shortcode('actual_comment_count', 'wpbeginner_comment_count'); 

This code creates a shortcode that you can now use anywhere on your website to display the total number of comments on your site excluding the trackbacks, pings, and unapproved comments.

Simply add the following shortcode anywhere you would like to display the comment count:

[actual_comment_count]

Here is how it looked on our test site:

Accurate comment count in WordPress

You can also use this shortcode in your WordPress theme templates using the following code:

<?php echo do_shortcode('[actual_comment_count]'); ?>

Showing comment count for a specific WordPress post

What if you wanted to display the accurate comment count for a single post or page? For instance, you may be creating a custom WordPress theme and want to display it in the comments template.

Simply add the following code to your functions.php file or using the custom code snippets plugin.

function wpbeginner_post_comment_count() { 
 
function comment_count( $count ) {
    if ( ! is_admin() ) {
global $post; 
        $comments_by_type = &separate_comments(get_comments('status=approve&post_id='. $post->ID ));
        return count($comments_by_type['comment']);
    } else {
        return $count;
    }
}
add_filter('get_comments_number', 'comment_count', 0);
 
$actual_comment_count = get_comments_number(); 
 
return $actual_comment_count;
 
}
 
add_shortcode('post_comment_count', 'wpbeginner_post_comment_count');

This code also creates a shortcode that you can use anywhere on your website to display the current post’s comment count.

Simply add the following shortcode to the post or page where you want to display comment count:

[post_comment_count]

Alternately, you can also use it inside your WordPress theme template files by using the following code:

<?php echo do_shortcode('[post_comment_count]'); ?>

We hope this article helped you learn how to easily display the most accurate comment count in WordPress. You may also want to see our guide on how to get more comments on your WordPress website.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

6 CommentsLeave a Reply

  1. Hi,
    on my website it only works in Gutenberg block “shortcode” not in “paragraph”. Am I doing something wrong?

  2. You guys always have the best tutorials. I searched Google for this because my site was prominently displaying inflated comment counts and found exactly what I was looking for!

    Thanks again!

  3. This code is helpful.

    I have paste this code in function.php and call this function in my template

    <?php echo comment_count(); ?>

    Thanks.

  4. 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.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.