Maybe wp_count_posts() is really not a regular template tag and is only used for admin stuff?
Has anyone gotten this to work yet? The codex page for it really needs some more information. Apparently it returns some sort of object, but I don’t know what to do with it to get it to display as the number of published posts.
http://codex.wordpress.org/Template_Tags/wp_count_posts
do it the old (pre-2.5) way.
$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
Thank you so much! It was counting posts and pages for some reason, so I used the following and it works. π
<?php $numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'"); echo $numpost; ?>
oh yes, of course… I forgot about pages being in there too.
excellent.
Wow!!!
Thanks a million. Great job spelling-it out with the final code, iridiax.
For the record, this method should replace — and is better than — the MsgCount plugin, and the Post Count plugin.
I actually use this plugin in the title of my blog. For example the title now is something like, “210 Songs” (where each post, is a song).
To take it one more step, can anyone help me insert Iridiax’s code, into my meta “Title” in my header.php?
The result I’m seeking is for my “Title” (up in the browser bar) to actually say “210 Songs” (which is, PostCount BlogTitle).
I don’t know how to do the PHP-inside-the-PHP.
Here’s where I would like to incorporate the postcount code, this is in the default Header.php,
<title>
<?php if (is_home () ) {
bloginfo('name'); echo " - "; bloginfo('description');
} elseif (is_single() || is_page() ) {
single_post_title(); echo " from "; bloginfo('name');
} else { wp_title('',true); }
?>
</title>
And again, here is the code I want to insert:
<?php $numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'"); echo $numpost; ?>
Regarding original problem:
<?php
$count_posts = wp_count_posts();
echo $count_posts->publish;
?>
Easy. No need for all that manual selection gibberish.
Otto for me that results in
Fatal error: Call to undefined function: wp_count_posts()
but this works
<?php $numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'"); echo $numpost; ?>
If I could get this to show up in my browser-bar (TITLE) that would be excellent. Anyone know?
Dgold: Are you running the latest version of WordPress? Because wp_count_posts is in the wp-includes/post.php file, and it is always loaded.
If you’re not running 2.5.1, upgrade.
I can confirm that the following does work for me (thanks Otto42):
<?php
$count_posts = wp_count_posts();
echo $count_posts->publish;
?>
Can you exclude post counts from a certain category, using this?
Thanks Otto42, I got that code working now, and replaced the older code in my theme.
Is it possible to show post count per user?
How can I count my posts under a particular tag?