wp_count_comments

The timeline below displays how wordpress function wp_count_comments has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.5

/**
 * Retrieves the total comment counts for the whole site or a single post.
 *
 * The comment stats are cached and then retrieved, if they already exist in the
 * cache.
 *
 * @see get_comment_count() Which handles fetching the live comment counts.
 *
 * @since 2.5.0
 *
 * @param int $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.
 *
 *     @type int $approved       The number of approved comments.
 *     @type int $moderated      The number of comments awaiting moderation (a.k.a. pending).
 *     @type int $spam           The number of spam comments.
 *     @type int $trash          The number of trashed comments.
 *     @type int $post-trashed   The number of comments for posts that are in the trash.
 *     @type int $total_comments The total number of non-trashed comments, including spam.
 *     @type int $all            The total number of pending or approved comments.
 * }
 */
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;
}

WordPress Version: 5.3

/**
 * Retrieves the total comment counts for the whole site or a single post.
 *
 * The comment stats are cached and then retrieved, if they already exist in the
 * cache.
 *
 * @see get_comment_count() Which handles fetching the live comment counts.
 *
 * @since 2.5.0
 *
 * @param int $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.
 *
 *     @type int|string $approved       The number of approved comments.
 *     @type int|string $moderated      The number of comments awaiting moderation (a.k.a. pending).
 *     @type int|string $spam           The number of spam comments.
 *     @type int|string $trash          The number of trashed comments.
 *     @type int|string $post-trashed   The number of comments for posts that are in the trash.
 *     @type int        $total_comments The total number of non-trashed comments, including spam.
 *     @type int        $all            The total number of pending or approved comments.
 * }
 */
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;
}

WordPress Version: 4.6

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int $post_id Optional. Post ID.
 * @return object|array Comment stats.
 */
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;
}

WordPress Version: 4.4

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int $post_id Optional. Post ID.
 * @return object|array Comment stats.
 */
function wp_count_comments($post_id = 0)
{
    $post_id = (int) $post_id;
    /**
     * Filter 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;
}

WordPress Version: 4.3

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb
 *
 * @param int $post_id Optional. Post ID.
 * @return object|array Comment stats.
 */
function wp_count_comments($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    /**
     * Filter the comments count for a given post.
     *
     * @since 2.7.0
     *
     * @param array $count   An empty array.
     * @param int   $post_id The post ID.
     */
    $stats = apply_filters('wp_count_comments', array(), $post_id);
    if (!empty($stats)) {
        return $stats;
    }
    $count = wp_cache_get("comments-{$post_id}", 'counts');
    if (false !== $count) {
        return $count;
    }
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    }
    $count = $wpdb->get_results("SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A);
    $total = 0;
    $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
    foreach ((array) $count as $row) {
        // Don't count post-trashed toward totals
        if ('post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved']) {
            $total += $row['num_comments'];
        }
        if (isset($approved[$row['comment_approved']])) {
            $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
        }
    }
    $stats['total_comments'] = $total;
    foreach ($approved as $key) {
        if (empty($stats[$key])) {
            $stats[$key] = 0;
        }
    }
    $stats = (object) $stats;
    wp_cache_set("comments-{$post_id}", $stats, 'counts');
    return $stats;
}

WordPress Version: 3.8

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int $post_id Optional. Post ID.
 * @return object Comment stats.
 */
function wp_count_comments($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    /**
     * Filter the comments count for a given post.
     *
     * @since 2.7.0
     *
     * @param array $count   An empty array.
     * @param int   $post_id The post ID.
     */
    $stats = apply_filters('wp_count_comments', array(), $post_id);
    if (!empty($stats)) {
        return $stats;
    }
    $count = wp_cache_get("comments-{$post_id}", 'counts');
    if (false !== $count) {
        return $count;
    }
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    }
    $count = $wpdb->get_results("SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A);
    $total = 0;
    $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
    foreach ((array) $count as $row) {
        // Don't count post-trashed toward totals
        if ('post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved']) {
            $total += $row['num_comments'];
        }
        if (isset($approved[$row['comment_approved']])) {
            $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
        }
    }
    $stats['total_comments'] = $total;
    foreach ($approved as $key) {
        if (empty($stats[$key])) {
            $stats[$key] = 0;
        }
    }
    $stats = (object) $stats;
    wp_cache_set("comments-{$post_id}", $stats, 'counts');
    return $stats;
}

WordPress Version: 3.7

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int $post_id Optional. Post ID.
 * @return object Comment stats.
 */
function wp_count_comments($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $stats = apply_filters('wp_count_comments', array(), $post_id);
    if (!empty($stats)) {
        return $stats;
    }
    $count = wp_cache_get("comments-{$post_id}", 'counts');
    if (false !== $count) {
        return $count;
    }
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    }
    $count = $wpdb->get_results("SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A);
    $total = 0;
    $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
    foreach ((array) $count as $row) {
        // Don't count post-trashed toward totals
        if ('post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved']) {
            $total += $row['num_comments'];
        }
        if (isset($approved[$row['comment_approved']])) {
            $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
        }
    }
    $stats['total_comments'] = $total;
    foreach ($approved as $key) {
        if (empty($stats[$key])) {
            $stats[$key] = 0;
        }
    }
    $stats = (object) $stats;
    wp_cache_set("comments-{$post_id}", $stats, 'counts');
    return $stats;
}