How To Display The Most Accurate Comment Count On WordPress

How To Display The Most Accurate Comment Count On WordPress

In WordPress, if you want to display comment count, it is suitable to say the most accurate comment count.

However, WordPress by default adds up trackbacks, pending moderation, spam, and pings with comments which increases the total comment count.

This Write-up will discuss how to display the most accurate comment count on your website.

Display Most Accurate Comment Count in WordPress by using codes.

1. In WordPress, open your template folder. In your  functions.php, paste the following code to filter out trackbacks and pings and only display the actual comment count to your users.

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;

}

You can show the most accurate comment count using the above code snippet.

2. This is another code you can use.

 In your WordPress site, place the following code:

 function hostkicker_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', 'hostkicker_comment_count'); 

The above code creates a shortcode that you can use anywhere on your website to show the total comments on your website. Also, it excludes the trackbacks, pings, and unapproved comments o your site.

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

[actual_comment_count]

Here is how it looks on site:

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

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

Using Plugin

If you don’t want to use the coding method, this method provides an easy way to display the total comment count on your site, including pingbacks and trackbacks.

So without any further delay, install and activate the Simple Blog Stats plugin.

In WordPress, go to plugins >> search for Simple Blog Stats plugin>> Install >> Activate plugin.

This plugin contains many shortcodes which you can use anywhere on your website to display different stats of your site.

For example, you can display a total number of posts, users, or comments.

For shortcodes, go to settings >> simple blog stats page.

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

[sbs_approved]

After that, you will see the most accurate comment count on the WordPress website.

Have a look.

The concern with this method is that it includes all the approved pings and trackbacks in your WordPress comment count.

To sum up, if you want to display the correct comment count on your website, then you have to use the code snippets or plugins. By default, WordPress adds up trackbacks, pending moderation, spam, and pings with comments, increasing the total comment count. You can use any of the above suitable methods to give the exact number to the users. 

Leave a Comment