Plugin Sale! Save 15% on pro plugins with discount code: FALL2020
Web Dev + WordPress + Security

Display the Total Number of WordPress Posts, Comments, and Categories

Would you like to display the total number of posts, comments, and categories for your WordPress-powered website? You can do it the easy way using my free WordPress plugin, Simple Blog Stats. Or you can display your statistics directly, by querying the WordPress database. Here is the code that can make it happen.

Update: The count posts part of this method should only be used for WordPress versions less than 2.5. For WordPress versions 2.5 and better, there is a new template tag for displaying the total number of posts.

Get number of posts, comments, and categories

Here is one way to query the WordPress database to get some basic count information. This can be used anywhere in your theme template, like sidebar.php or even better tucked neatly inside of a custom function (added via functions.php or plugin). The following code snippet is kept basic and should be self-explanatory:

<?php

$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts); 

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

$numcats = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories");
if (0 < $numcats) $numcats = number_format($numcats);

?>

<p><?php printf(__('There are currently %1$s <a href="%2$s" title="Posts">posts</a> and %3$s <a href="%4$s" title="Comments">comments</a>, contained within %5$s <a href="%6$s" title="categories">categories</a>.'), $numposts, 'edit.php',  $numcomms, 'edit-comments.php', $numcats, 'categories.php'); ?></p>

As another example, here is a slightly modified version of the code, customized for use here at Perishable Press:

<?php

$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts); 

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

$numcats = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->categories");
if (0 < $numcats) $numcats = number_format($numcats);

?>

<?php echo $numposts . ' posts and ' . $numcomms . ' comments in ' . $numcats . ' categories'; ?>

Any questions? Hit me in the comments below! :)

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
.htaccess made easy: Improve site performance and security.

2 responses to “Display the Total Number of WordPress Posts, Comments, and Categories”

  1. Thanks for this info, just what I was looking for. I’m glad that I didn’t have to use yet another plugin to accomplish this. :)

  2. Jeff Starr
    Perishable 2007/09/28 7:55 pm

    My pleasure, don! Thanks for the positive feedback :)

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
GA Pro: Add Google Analytics to WordPress like a pro.
Thoughts
Part of my success is from being able to look ahead down the road and see things coming before they actually get here.
Spent some time refreshing my business portfolio at MonzillaMedia.com :)
Tried lots of apps for making animated GIFs from screen captures, so far Kap is my favorite. For example the GIF on this post.
Autumn is my favorite time of year.
Stoked! Had a great interview with Eric over at Speckyboy.com :)
Air finally clearing here in WA. Feeling grateful to breathe again. #oxygenmatters
Past week here in WA state has been hellish. So much smoke, like living in a chimney.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.