How to: Display the total number of comments on your WordPress blog

Some days ago, I shown you a great hack about getting the total number of posts published on your WordPress blog. Today, let’s do the same with comments.

This simple hack works exactly as the "get total number of posts" hack works: We're using the $wpdb object and make a custom query to MySQL:

<?php
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if (0 < $numcomms) $numcomms = number_format($numcomms);
?>

Right now, the $numcomms variable contains the total number of comments posted on your WordPress blog. To display this number, simply do something like:

<?php echo "There's ".$numcomms." comments on this blog";

11 Responses

Oct 18 2008 23:58

Use wp_count_comments() for that. It returns an Array with approved, moderated, spam, total_comments
Here is a post with some expamples.

Oct 19 2008 00:04

I didn’t knew that function, thanks!

Dec 10 2008 04:12

You need to close the php tags in the second textarea. ;)

Dec 10 2008 12:03

@Paul Davis: That’s true, I’ll correct it soon. Thanks for letting me know!

Oct 16 2009 19:38

I can’t find this function – wp_count_comments() in WordPress documentation and it doesn’t work on my blog, or maybe I did something wrong. I have WP 2.8.4

Oct 16 2009 20:21

Hallo agin, I found a very simple plugin for this – Comment Count by Nick Momrik. Instalation: only activate the plugin and place mdv_comment_count() in template. It counts only approved comments. It works with WP 2.8.4

Jun 20 2010 15:26

I want to show the total no. of comments into footer like
Total Comments : 56 ( or Whatever)
how can I achieve this while I’m using the intense depate plugin..
Note: I don’t want to use intense debate widget… I want to get it manually into my template…

Any Idea?

Thanks,

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required