Display the Total Number of WordPress Posts, Comments, and Categories
Post #190 categorized as Function, WordPress, last updated on Feb 17, 2009
Tagged with comments, php, posts, statistics, tips, tricks, WordPress
Would you like to display the total number of posts, comments, and categories for your WordPress-powered website? Here is the code that can make it happen 1!
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 built-in function for displaying the total number of posts. See The WordPress Codex for more information.
<?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>
Here is a modified version of the code, customized for our 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'; ?>
Footnotes
- 1 Get the plugin that does it for you! » BlogStats PCC Plugin for WordPress
Share this..
Related articles
- Super Loop: Exclude Specific Categories and Display any Number of Posts
- WordPress Tip: Disable Comments in Old Posts via PHP
- Horizontally Sequenced Display Order for WordPress Posts in Two Columns
- Better WordPress Archives via Dynamic Triple Column Layout
- How to Display Your Twitter Posts on Your WordPress Blog
- WordPress Discussion Management: Enable or Disable Comments and Pingbacks via SQL
- Fruit Loop: Separate any Number of Odd and Even Posts from any Category in WordPress
#1 — don
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. :)