• 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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try <?php wp_count_posts( $type, $perm ); ?>

    Thread Starter Jo?o Sardinha

    (@johnsardine)

    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

    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

    Thread Starter Jo?o Sardinha

    (@johnsardine)

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

    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; //
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display Custom Post Type Post Count’ is closed to new replies.