Support » Fixing WordPress » Display post count wp_count_posts

  • Resolved iridiax


    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.

    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)
  • 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.

    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.



    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,

    <?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); }

    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)

    (@otto42) Admin

    Regarding original problem:

    $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?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    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):

    $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?

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Display post count wp_count_posts’ is closed to new replies.