Welcome to the new design! Please report any bugs or issues, thanks :)
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 = Designer. Developer. Producer. Writer. Editor. Etc.
BBQ Pro: The fastest firewall to protect your WordPress.

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 »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
2 things I hate to see in stylesheets: _ and #
Love VLC media player but it fails miserably when it comes to randomizing large collections of mp3 and other files.
Dashlane redesigned, stating proudly they "removed all filigree". Should have kept it; the app now looks generic and boring. Killed your identity.
Working on integration for setaPDF + EDD on the new books subdomain. Good times.
Toggle visibility of hidden files on Mac: Cmd + Shift + .
Great tool for checking browser caching for web pages and all included files.
The new minimalist design styled by 14KB of CSS (uncompressed and un-minified). That covers 850+ posts and pages spanning 15 years of diverse content.