Display number of posts from database in WordPress

WordPress does not have a function to do that by default, but with a little work you can get this example. With this code actualy whe will learn wordpress what to count, and to print how many posts are in database.

We are using the $wpdb object to make a custom query to WordPress database:

<?php $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. All whe have to do is to display it ( in sidebar, in content, etc.. ).

<?php echo $numposts.’ has been published in database’; ?>

Leave a Comment

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>