Jump Menu : Content | Explore | Comments | Search | Home | Sitemap | Contact | Login | Access.

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? Here is the code that can make it happen 1!

<?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

Related articles

About this article

This is article #190, posted by Jeff Starr on Monday, August 28, 2006 @ 09:51am. Categorized as Function, WordPress, and tagged with comments, php, posts, statistics, tips, tricks, WordPress. Updated on November 04, 2007. Visited 19629 times. 3 Responses »

BookmarkSubscribeExplore

« Accessibility Notes Plus • Up • BlogStats PCC Plugin »


3 Responses

1 • September 28, 2007 at 8:47 am — don says:

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 • September 28, 2007 at 7:55 pm — Perishable says:

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

[ Comments are closed for this post. ]

Trackbacks / Pingbacks

  1. WordPress Wednesday: New Security Release, Updated WordPressMU, More WordPress Plugins, and Time to Get Naked at The Blog Herald

Set CSS to lite theme
Set CSS to dark theme