WordPress.org

Forums

[resolved] Display post count wp_count_posts (16 posts)

  1. iridiax
    Member
    Posted 7 years ago #

    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

    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?

  2. iridiax
    Member
    Posted 7 years ago #

    Maybe wp_count_posts() is really not a regular template tag and is only used for admin stuff?

  3. iridiax
    Member
    Posted 6 years ago #

    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

  4. Ivovic
    Member
    Posted 6 years ago #

    do it the old (pre-2.5) way.

    $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
  5. iridiax
    Member
    Posted 6 years ago #

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

  6. Ivovic
    Member
    Posted 6 years ago #

    oh yes, of course... I forgot about pages being in there too.

    excellent.

  7. Dgold
    Member
    Posted 6 years ago #

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

  8. Dgold
    Member
    Posted 6 years ago #

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

  9. Regarding original problem:

    <?php
    $count_posts = wp_count_posts();
    echo $count_posts->publish;
    ?>

    Easy. No need for all that manual selection gibberish.

  10. Dgold
    Member
    Posted 6 years ago #

    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?

  11. 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.

  12. iridiax
    Member
    Posted 6 years ago #

    I can confirm that the following does work for me (thanks Otto42):

    <?php
    $count_posts = wp_count_posts();
    echo $count_posts->publish;
    ?>
  13. karlbiffaboy
    Member
    Posted 6 years ago #

    Can you exclude post counts from a certain category, using this?

  14. Dgold
    Member
    Posted 6 years ago #

    Thanks Otto42, I got that code working now, and replaced the older code in my theme.

  15. Dave Bergschneider
    Member
    Posted 6 years ago #

    Is it possible to show post count per user?

  16. aldolat
    Member
    Posted 6 years ago #

    How can I count my posts under a particular tag?

Topic Closed

This topic has been closed to new replies.

About this Topic