How to: Display the total number of posts on your WordPress blog

Wouldn’t it be nice to be able to display the total number of posts published on your WordPress blog? WordPress don’t have a function to do that by default, but happilly this hack is here to help.

Here's the code: We're using the $wpdb object to make a custom query to WordPress database:

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

Right now, the $numposts variable contains the total number of posts. You now just have to display it where you want:

<?php echo $numposts.' has been published since January 12, 2008'; ?>

Thanks to Perishable Press for this great hack!

One Response

Dec 16 2011 02:03

You could also use the built-in wp_count_posts() which returns posts of the specified type, permission and groups by status. http://codex.wordpress.org/Function_Reference/get_query_var

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required