Forums

Display Custom Post Type Post Count (6 posts)

  1. João Sardinha
    Member
    Posted 11 months ago #

    Hello,

    i need to output the number of posts in a custom post type so i can later output other stuff based on the number of posts.

    <?php query_posts(array('post_type' => 'galleries')); ?>
    
    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); $count++; ?>
    
    <?php echo $count; ?>
    
    <?php endwhile;?>
    <?php endif; ?>		
    
    <?php wp_reset_query(); ?>

    So far i have that, but it only outputs me a number per post, which is normal, but i want a variable that contains the number of posts in that post type so i can use conditional comments later

    Any help?

    Thank you

  2. vankaa
    Member
    Posted 11 months ago #

    Try <?php wp_count_posts( $type, $perm ); ?>

  3. João Sardinha
    Member
    Posted 11 months ago #

    Thank you, that seems to be what im looking for, however when i try

    <?php
    $count_posts = wp_count_posts('posts');
    echo $count_posts;
    ?>

    it outputs an error:

    Catchable fatal error: Object of class stdClass could not be converted to string in mysite/index.php on line 92

  4. vankaa
    Member
    Posted 11 months ago #

    If you want all posts, than leave it default or write 'post'. If you want something specific like 'post_type' => 'galleries', than write it in. You can read the documentation

  5. João Sardinha
    Member
    Posted 11 months ago #

    i am aware of that thank you, but it outputs the above error when i try

  6. jkulig
    Member
    Posted 2 months ago #

    wp_count_posts() returns an object. Make sure you reference one of its properties:

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

Reply

You must log in to post.

About this Topic