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, trackbacks and pings. This makes it difficult to show the exact comment count on your WordPress site.

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

Display accurate comment count in WordPress

Method 1. Display Comment Count in WordPress Using Plugin

The easiest way to show your total number of approved comments is by using the Simple Blog Stats plugin.

However, this plugin doesn’t include pings and trackbacks in its count.

Many websites turn off trackbacks and pingbacks in an attempt to block spam comments in WordPress, so this may not be a big problem for your website.

However, if you want to include trackbacks and pings in your total comment count, then we recommend using method 2 instead.

To get started with this method, 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.

Upon activation, you can see all the different shortcodes by going to the Settings » Simple Blog Stats page.

Simple blog stats shortcodes

To show the total count of all approved comments on your website, simply use the following shortcode: [sbs_approved]

For more information on how to place the shortcode, please see our guide on how to add a shortcode in WordPress.

After adding the shortcode, you can visit your WordPress website to see the total count of all approved comments.

Comment count via plugin

Method 2. Manually Display Most Accurate Comment Count in WordPress

If you want to include pingbacks and trackbacks in the total comment count, then you’ll need to add custom code to your website.

Typically, you would need to add custom code snippets to your theme’s functions.php file.

However, we don’t recommend this method as even a small mistake in your code could cause a number of common WordPress errors, or even break your site entirely.

That’s why we recommend using WPCode. It is the easiest and safest way to add custom code in WordPress without having to edit any core WordPress theme files.

The first thing you need to do is install and activate the free WPCode plugin on your website. For more details, see our step-by-step guide on how to install a WordPress plugin.

Upon activation, go to Code Snippets » Add Snippet.

Showing the total comment count using code

Here, you’ll see all of WPCode’s ready-made snippets that you can add to your site.

Since we’re creating our own snippet, hover your mouse over ‘Add Your Custom Code,’ and then click ‘Use snippet.’

Displaying an accurate comment count using WPCode

To start, enter a title for the custom code snippet. This can be anything that helps you identify the snippet in your WordPress dashboard.

After that, open the ‘Code Type’ dropdown and choose ‘PHP Snippet.’

Adding a custom PHP code snippet to WordPress

Once you’ve done that, simply paste the following snippet into the code editor:

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

Below the code box, you will see insertion options. We want to use our shortcode on any page, post, or widget-ready area, so open the ‘Location’ dropdown menu and then click on ‘Run Everywhere.’

Using the PHP code across your WordPress website

Then, you’re ready to scroll to the top of the screen and click on the ‘Inactive’ toggle so it changes to ‘Active.’

Once you’ve done that, click on ‘Save snippet’ to make this snippet live.

Saving the total comment count snippet

You now have a shortcode that you can use to show the total number of comments on your website including trackbacks, pings, and unapproved comments.

Simply add the [actual_comment_count] shortcode anywhere you want to display the comment count. For more information on how to place the shortcode, please see our guide on how to add a shortcode in WordPress.

Showing Comment Count for a Specific WordPress Post

What if you wanted to display the accurate comment count for a single post or page?

You can simply add the following PHP code snippet to WPCode following the same process described above:

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 creates a [post_comment_count] shortcode that you can add to any page or post.

We hope this article helped you learn how to easily display the most accurate comment count in WordPress. Next, you can see our guide on how to increase your blog traffic, or see our expert pick of the best contact form plugins for WordPress.

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.