wp_count_comments( int $post_id )
Retrieves the total comment counts for the whole site or a single post.
Description
The comment stats are cached and then retrieved, if they already exist in the cache.
See also
get_comment_count(): Which handles fetching the live comment counts.
Parameters
- $post_id
-
(Optional) Restrict the comment counts to the given post. Default 0, which indicates that comment counts for the whole site will be retrieved.
Return
(stdClass) The number of comments keyed by their status.<br>
- 'approved'
(int) The number of approved comments.<br> - 'moderated'
(int) The number of comments awaiting moderation (a.k.a. pending).<br> - 'spam'
(int) The number of spam comments.<br> - 'trash'
(int) The number of trashed comments.<br> - 'post-trashed'
(int) The number of comments for posts that are in the trash.<br> - 'total_comments'
(int) The total number of non-trashed comments, including spam.<br> - 'all'
(int) The total number of pending or approved comments.<br>
Source
File: wp-includes/comment.php
function wp_count_comments( $post_id = 0 ) {
$post_id = (int) $post_id;
/**
* Filters the comments count for a given post.
*
* @since WP-2.7.0
*
* @param array $count An empty array.
* @param int $post_id The post ID.
*/
$filtered = apply_filters( 'wp_count_comments', array(), $post_id );
if ( ! empty( $filtered ) ) {
return $filtered;
}
$count = wp_cache_get( "comments-{$post_id}", 'counts' );
if ( false !== $count ) {
return $count;
}
$stats = get_comment_count( $post_id );
$stats['moderated'] = $stats['awaiting_moderation'];
unset( $stats['awaiting_moderation'] );
$stats_object = (object) $stats;
wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' );
return $stats_object;
}
Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |