wp_count_comments( int $post_id )

Retrieves the total comment counts for the whole site or a single post.


Description Description

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

See also See also


Top ↑

Parameters Parameters

$post_id

(int) (Optional) Restrict the comment counts to the given post. Default 0, which indicates that comment counts for the whole site will be retrieved.


Top ↑

Return Return

(stdClass) The number of comments keyed by their status.

  • 'approved'
    (int|string) The number of approved comments.
  • 'moderated'
    (int|string) The number of comments awaiting moderation (a.k.a. pending).
  • 'spam'
    (int|string) The number of spam comments.
  • 'trash'
    (int|string) The number of trashed comments.
  • 'post-trashed'
    (int|string) The number of comments for posts that are in the trash.
  • 'total_comments'
    (int) The total number of non-trashed comments, including spam.
  • 'all'
    (int) The total number of pending or approved comments.

Top ↑

Source 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 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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Retrieve comment count for a post

    <?php
    $comments_count = wp_count_comments( 2492 );
    echo "Comments for post 2492 <br />";
    echo "Comments in moderation: " . $comments_count->moderated . "<br />"; 
    echo "Comments approved: " . $comments_count->approved . "<br />";
    echo "Comments in Spam: " . $comments_count->spam . "<br />";
    echo "Comments in Trash: " . $comments_count->trash . "<br />";
    echo "Total Comments: " . $comments_count->total_comments . "<br />";
    ?>
    
    
  2. Skip to note 2 content
    Contributed by Codex

    Default usage
    Retrieve comment count for a site.

    <?php
    $comments_count = wp_count_comments();
    echo "Comments for site <br />";
    echo "Comments in moderation: " . $comments_count->moderated . "<br />"; 
    echo "Comments approved: " . $comments_count->approved . "<br />";
    echo "Comments in Spam: " . $comments_count->spam . "<br />";
    echo "Comments in Trash: " . $comments_count->trash . "<br />";
    echo "Total Comments: " . $comments_count->total_comments . "<br />";
    ?>
    
    

You must log in before being able to contribute a note or feedback.