DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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 Comment

  1. 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

Leave a Comment

Your email address will not be published. Required fields are marked *