DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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";

2 Comments

  1. I am about to try this out as i’m hoping to display my total comment numbers!

  2. If you are going to use this with a plugin make sure you add a call to the global variable: global $wpdb; to the give us access to the DB

Leave a Comment

Your email address will not be published. Required fields are marked *