Title: Display post count wp_count_posts
Last modified: August 19, 2016

---

# Display post count wp_count_posts

 *  Resolved [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/)
 * I want to display the number of published posts on one of my template files (
   index.php), and I have tried using the wp_count_posts template tag, but it does
   not seem to work.
 * [http://codex.wordpress.org/Template_Tags/wp_count_posts](http://codex.wordpress.org/Template_Tags/wp_count_posts)
 * The following versions display nothing:
 *     ```
       <?php $published_posts=wp_count_posts(); ?>
   
       <?php $published_posts=wp_count_posts('post','publish'); ?>
   
       <?php wp_count_posts(); ?>
   
       <?php wp_count_posts('post','publish'); ?>
       ```
   
 * While these versions display **Object**:
 *     ```
       <?php $published_posts=wp_count_posts(); echo $published_posts; ?>
   
       <?php $published_posts=wp_count_posts('post','publish'); echo $published_posts; ?>
       ```
   
 * What do I need to fix to make it work?

Viewing 15 replies - 1 through 15 (of 15 total)

 *  Thread Starter [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-755928)
 * Maybe wp_count_posts() is really not a regular template tag and is only used 
   for admin stuff?
 *  Thread Starter [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756100)
 * 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](http://codex.wordpress.org/Template_Tags/wp_count_posts)
 *  [ivovic](https://wordpress.org/support/users/ivovic/)
 * (@ivovic)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756101)
 * do it the old (pre-2.5) way.
 *     ```
       $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
       ```
   
 *  Thread Starter [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756102)
 * 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; ?>`
 *  [ivovic](https://wordpress.org/support/users/ivovic/)
 * (@ivovic)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756103)
 * oh yes, of course… I forgot about pages being in there too.
 * excellent.
 *  [Dgold](https://wordpress.org/support/users/dgold/)
 * (@dgold)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756122)
 * 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).
 *  [Dgold](https://wordpress.org/support/users/dgold/)
 * (@dgold)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756123)
 * 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; ?>`
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 10 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756124)
 * Regarding original problem:
 *     ```
       <?php
       $count_posts = wp_count_posts();
       echo $count_posts->publish;
       ?>
       ```
   
 * Easy. No need for all that manual selection gibberish.
 *  [Dgold](https://wordpress.org/support/users/dgold/)
 * (@dgold)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756126)
 * 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?
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 10 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756127)
 * 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.
 *  Thread Starter [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756128)
 * I can confirm that the following does work for me (thanks Otto42):
 *     ```
       <?php
       $count_posts = wp_count_posts();
       echo $count_posts->publish;
       ?>
       ```
   
 *  [karlbiffaboy](https://wordpress.org/support/users/karlbiffaboy/)
 * (@karlbiffaboy)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756147)
 * Can you exclude post counts from a certain category, using this?
 *  [Dgold](https://wordpress.org/support/users/dgold/)
 * (@dgold)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756151)
 * Thanks Otto42, I got that code working now, and replaced the older code in my
   theme.
 *  [Dave Bergschneider](https://wordpress.org/support/users/dave-bergschneider/)
 * (@dave-bergschneider)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756192)
 * Is it possible to show post count per user?
 *  [Aldo Latino](https://wordpress.org/support/users/aldolat/)
 * (@aldolat)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756195)
 * How can I count my posts under a particular tag?

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Display post count wp_count_posts’ is closed to new replies.

## Tags

 * [wp_count_posts](https://wordpress.org/support/topic-tag/wp_count_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 15 replies
 * 7 participants
 * Last reply from: [Aldo Latino](https://wordpress.org/support/users/aldolat/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/display-post-count-wp_count_posts/#post-756195)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
