On this page
wp_count_comments( int $post_id ): stdClass
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_idint 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.
approvedintThe number of approved comments.moderatedintThe number of comments awaiting moderation (a.k.a. pending).spamintThe number of spam comments.trashintThe number of trashed comments.post-trashedintThe number of comments for posts that are in the trash.total_commentsintThe total number of non-trashed comments, including spam.allintThe total number of pending or approved comments.
Source
File: wp-includes/comment.php. View all references
function wp_count_comments( $post_id = 0 ) {
$post_id = (int) $post_id;
/**
* Filters the comments count for a given post or the whole site.
*
* @since 2.7.0
*
* @param array|stdClass $count An empty array or an object containing comment counts.
* @param int $post_id The post ID. Can be 0 to represent the whole site.
*/
$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;
}
Hooks
- apply_filters( 'wp_count_comments',
array|stdClass $count ,int $post_id ) -
Filters the comments count for a given post or the whole site.
Related
Uses
| Uses | Description |
|---|---|
| wp_cache_set() wp-includes/cache.php | Saves the data to the cache. |
| get_comment_count() wp-includes/comment.php | Retrieves the total comment counts for the whole site or a single post. |
| wp_cache_get() wp-includes/cache.php | Retrieves the cache contents from the cache by key and group. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| wp_dashboard_right_now() wp-admin/includes/dashboard.php | Dashboard widget that displays some basic stats about the site. |
| _wp_ajax_delete_comment_response() wp-admin/includes/ajax-actions.php | Sends back current comment total and new page links if they need to be updated. |
| wp_ajax_replyto_comment() wp-admin/includes/ajax-actions.php | Ajax handler for replying to a comment. |
| WP_Comments_List_Table::get_views() wp-admin/includes/class-wp-comments-list-table.php | |
| wp_admin_bar_comments_menu() wp-includes/admin-bar.php | Adds edit comments link with awaiting moderation count bubble. |
| wp_xmlrpc_server::wp_getCommentCount() wp-includes/class-wp-xmlrpc-server.php | Retrieve comment count. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_count_comments