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!

28 Responses

Oct 14 2008 11:05

Excellent recipe! Thanks for sharing this!

Oct 15 2008 00:20

Is it possible to display the total number of comments, categorie and tags? And even better: How many posts today?

Oct 16 2008 10:22

@Dan: Good idea, I’ll think about it for a new recipe :)

Oct 18 2008 20:59

Can you provide a little more detail for us beginners. Where does each of these blocks of code go?

Oct 18 2008 22:34

@Larry: No problem: The first code snippet define a php variable, and the second display the result of this variable.
So, you got to paste both theses codes in your template, where you want “X posts since I launched my blog” to appear.

Let me know if you have trouble.

Nov 10 2008 01:01

Hi, just to mention that the first snippet of code isn’t inside a php thingy. It took me a while to notice it and it was driving me nuts to put it in my template. But, also, I’m no programmer. It works for me.

Nov 10 2008 01:16

@V for Vancouver: you’re right, experimented programmer will notice instantly but that may be hard for novices. I’m correcting it :)

Dec 07 2008 18:16

Hello :)

it is possible to limit this query at only one category?

if yes, how to do that?

++

Jan 24 2009 02:23
Jan 24 2009 08:51

@Rarst: I know, but this function was introduced with WP 2.7 I think. Back in october when I wrote this recipe, WP 2.7 wasn’t released ;)

Jan 24 2009 09:05

@Jean-Baptiste Jung

Yeah, I know it’s recent. Just think it’s worth to keep recipes updated with easiest way to do something instead of keeping legacy code.

Jan 28 2009 09:29

Thanks Rarst for pointing out that with 2.7 comes a function for post counting.

Feb 21 2009 23:33

Very nice but you are now counting page and posts.

Better us this.

get_var(“SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_type = ‘post’”);
if (0

greetz

Feb 21 2009 23:34

Very nice but you are now counting page and posts.

Better us this.

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

greetz

May 23 2009 14:05

I think there is a plug in for that?

Jul 09 2009 16:31

What if you wanted to exclude a specific category? I’ve tried adding AND post_category != ‘3′ but it didn’t work. Am I supposed to use something else?

Aug 24 2009 12:56

Sorry guys but this QS sux. WordPress puts %autosave%, %revision% and page in the same bag so you’ll never get correct result. In spite of the fact that there’s built-in function which does the same job, here’s my version of displaying total num of posts

$numposts = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_type = ‘post’ AND post_name NOT LIKE ‘%revision%’ AND post_name NOT LIKE ‘%autosave%’” );

Jan 22 2010 09:53

ok. and code from numbers of post publised today ?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required