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.
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.
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.
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.
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.’
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.’
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.’
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.
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.
Chris says
Hi,
on my website it only works in Gutenberg block “shortcode” not in “paragraph”. Am I doing something wrong?
insitedesignlab says
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!
imranansari says
This code is helpful.
I have paste this code in function.php and call this function in my template
<?php echo comment_count(); ?>
Thanks.
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.
Tricyclic Antidepressants says
Very interesting article and very good blog. Very much interested to know)
I will add this blog to RSS Reader
Wordpress Developer says
I was looking similar a wordpress plugin. This code very useful for me. Thank you.