get_comment_count

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

WordPress Version: 6.4

/**
 * Retrieves the total comment counts for the whole site or a single post.
 *
 * @since 2.0.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 int[] {
 *     The number of comments keyed by their status.
 *
 *     @type int $approved            The number of approved comments.
 *     @type int $awaiting_moderation 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 get_comment_count($post_id = 0)
{
    $post_id = (int) $post_id;
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    $args = array('count' => true, 'update_comment_meta_cache' => false, 'orderby' => 'none');
    if ($post_id > 0) {
        $args['post_id'] = $post_id;
    }
    $mapping = array('approved' => 'approve', 'awaiting_moderation' => 'hold', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
    $comment_count = array();
    foreach ($mapping as $key => $value) {
        $comment_count[$key] = get_comments(array_merge($args, array('status' => $value)));
    }
    $comment_count['all'] = $comment_count['approved'] + $comment_count['awaiting_moderation'];
    $comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];
    return array_map('intval', $comment_count);
}

WordPress Version: 6.1

/**
 * Retrieves the total comment counts for the whole site or a single post.
 *
 * @since 2.0.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 int[] {
 *     The number of comments keyed by their status.
 *
 *     @type int $approved            The number of approved comments.
 *     @type int $awaiting_moderation 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 get_comment_count($post_id = 0)
{
    $post_id = (int) $post_id;
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    $args = array('count' => true, 'update_comment_meta_cache' => false);
    if ($post_id > 0) {
        $args['post_id'] = $post_id;
    }
    $mapping = array('approved' => 'approve', 'awaiting_moderation' => 'hold', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
    $comment_count = array();
    foreach ($mapping as $key => $value) {
        $comment_count[$key] = get_comments(array_merge($args, array('status' => $value)));
    }
    $comment_count['all'] = $comment_count['approved'] + $comment_count['awaiting_moderation'];
    $comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];
    return array_map('intval', $comment_count);
}

WordPress Version: 5.9

/**
 * Retrieves the total comment counts for the whole site or a single post.
 *
 * Unlike wp_count_comments(), this function always returns the live comment counts without caching.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @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 int[] {
 *     The number of comments keyed by their status.
 *
 *     @type int $approved            The number of approved comments.
 *     @type int $awaiting_moderation 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 get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare('WHERE comment_post_ID = %d', $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'trash':
                $comment_count['trash'] = $row['total'];
                break;
            case 'post-trashed':
                $comment_count['post-trashed'] = $row['total'];
                break;
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case '1':
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            case '0':
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return array_map('intval', $comment_count);
}

WordPress Version: 5.5

/**
 * Retrieves the total comment counts for the whole site or a single post.
 *
 * Unlike wp_count_comments(), this function always returns the live comment counts without caching.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @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 array() {
 *     The number of comments keyed by their status.
 *
 *     @type int $approved            The number of approved comments.
 *     @type int $awaiting_moderation 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 get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare('WHERE comment_post_ID = %d', $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'trash':
                $comment_count['trash'] = $row['total'];
                break;
            case 'post-trashed':
                $comment_count['post-trashed'] = $row['total'];
                break;
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case '1':
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            case '0':
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return array_map('intval', $comment_count);
}

WordPress Version: 5.3

/**
 * Retrieves the total comment counts for the whole site or a single post.
 *
 * Unlike wp_count_comments(), this function always returns the live comment counts without caching.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @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 array() {
 *     The number of comments keyed by their status.
 *
 *     @type int|string $approved            The number of approved comments.
 *     @type int|string $awaiting_moderation 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 get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare('WHERE comment_post_ID = %d', $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'trash':
                $comment_count['trash'] = $row['total'];
                break;
            case 'post-trashed':
                $comment_count['post-trashed'] = $row['total'];
                break;
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case '1':
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            case '0':
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return $comment_count;
}

WordPress Version: 5.1

/**
 * The amount of comments in a post or total comments.
 *
 * A lot like wp_count_comments(), in that they both return comment stats (albeit with different types).
 * The wp_count_comments() actually caches, but this function does not.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
 * @return array The amount of spam, approved, awaiting moderation, and total comments.
 */
function get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare('WHERE comment_post_ID = %d', $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'trash':
                $comment_count['trash'] = $row['total'];
                break;
            case 'post-trashed':
                $comment_count['post-trashed'] = $row['total'];
                break;
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case '1':
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            case '0':
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return $comment_count;
}

WordPress Version: 4.6

/**
 * The amount of comments in a post or total comments.
 *
 * A lot like wp_count_comments(), in that they both return comment stats (albeit with different types).
 * The wp_count_comments() actually caches, but this function does not.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
 * @return array The amount of spam, approved, awaiting moderation, and total comments.
 */
function get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'trash':
                $comment_count['trash'] = $row['total'];
                break;
            case 'post-trashed':
                $comment_count['post-trashed'] = $row['total'];
                break;
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case '1':
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            case '0':
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return $comment_count;
}

WordPress Version: 4.4

/**
 * The amount of comments in a post or total comments.
 *
 * A lot like {@link wp_count_comments()}, in that they both return comment
 * stats (albeit with different types). The {@link wp_count_comments()} actual
 * caches, but this function does not.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
 * @return array The amount of spam, approved, awaiting moderation, and total comments.
 */
function get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array('approved' => 0, 'awaiting_moderation' => 0, 'spam' => 0, 'trash' => 0, 'post-trashed' => 0, 'total_comments' => 0, 'all' => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'trash':
                $comment_count['trash'] = $row['total'];
                break;
            case 'post-trashed':
                $comment_count['post-trashed'] = $row['total'];
                break;
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case '1':
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            case '0':
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                $comment_count['all'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return $comment_count;
}

WordPress Version: 4.1

/**
 * The amount of comments in a post or total comments.
 *
 * A lot like {@link wp_count_comments()}, in that they both return comment
 * stats (albeit with different types). The {@link wp_count_comments()} actual
 * caches, but this function does not.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
 * @return array The amount of spam, approved, awaiting moderation, and total comments.
 */
function get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array("approved" => 0, "awaiting_moderation" => 0, "spam" => 0, "total_comments" => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count["total_comments"] += $row['total'];
                break;
            case 1:
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case 0:
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return $comment_count;
}

WordPress Version: 3.7

/**
 * The amount of comments in a post or total comments.
 *
 * A lot like {@link wp_count_comments()}, in that they both return comment
 * stats (albeit with different types). The {@link wp_count_comments()} actual
 * caches, but this function does not.
 *
 * @since 2.0.0
 * @uses $wpdb
 *
 * @param int $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
 * @return array The amount of spam, approved, awaiting moderation, and total comments.
 */
function get_comment_count($post_id = 0)
{
    global $wpdb;
    $post_id = (int) $post_id;
    $where = '';
    if ($post_id > 0) {
        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    }
    $totals = (array) $wpdb->get_results("\n\t\tSELECT comment_approved, COUNT( * ) AS total\n\t\tFROM {$wpdb->comments}\n\t\t{$where}\n\t\tGROUP BY comment_approved\n\t", ARRAY_A);
    $comment_count = array("approved" => 0, "awaiting_moderation" => 0, "spam" => 0, "total_comments" => 0);
    foreach ($totals as $row) {
        switch ($row['comment_approved']) {
            case 'spam':
                $comment_count['spam'] = $row['total'];
                $comment_count["total_comments"] += $row['total'];
                break;
            case 1:
                $comment_count['approved'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            case 0:
                $comment_count['awaiting_moderation'] = $row['total'];
                $comment_count['total_comments'] += $row['total'];
                break;
            default:
                break;
        }
    }
    return $comment_count;
}