Display Total Number of Posts on Your WordPress Blog

by Hiroshi on November 12, 2008

in Hacks, WordPress

This tutorial show you that how to display the total number of posts published on your WordPress blog. Although there are number of WpPlugins are available for stats, but what about creating your own stats for just Total Number of Posts as clean as original. This function is not by default in WordPress software. But we can get the job done by the following method.

There can be many other ways to do so but we use some simple way by 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);

The $numposts variable contains the total number of posts in above example. You can display it at your blog where ever you want by using following lines:

<?php echo $numposts.' Total Posts Published'; ?>

Just use all the code in the sidebar, I would recommend.

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

Related Posts

Previous post:

Next post: