Can’t show comments count per post outside loop

To print just the total number of comments for a given post ID, use the count argument:

echo get_comments(
    array (
        // post ID
        'post_id' => 149,
        // return just the total number
        'count'   => TRUE
    )
);

or just use

// Argument: Post ID
echo get_comment_count( 149 );

To get the total number of all comments of all posts on the current page, you can use the comment_count property of the post objects and sum them up:

echo array_sum(
    wp_list_pluck( $GLOBALS['wp_query']->posts, 'comment_count' )
);

Leave a Comment