WordPress at a glance

wp_count_comments() WP 1.0

Retrieve total comments for blog or single post.

The properties of the returned object contain the 'moderated', 'approved', and spam comments for either the entire blog or single post. Those properties contain the amount of comments that match the status. The 'total_comments' property contains the integer of total comments.

The comment stats are cached and then retrieved, if they already exist in the cache.

Hooks from function:
Return

Object/Array. Comment stats.

Usage

wp_count_comments( $post_id );
$post_id(int)
Post ID.
Default: 0

Code of wp_count_comments: wp-includes/comment.php VER 4.9.8

<?php
function wp_count_comments( $post_id = 0 ) {
	$post_id = (int) $post_id;

	/**
	 * Filters the comments count for a given post.
	 *
	 * @since 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;
}

Related Functions

From tag: counts (counting of any)

More from category: Comments

No comments
    Hello, !     Log In . Register