WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

get_comment_count() WP 2.0.0

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.

Хуков нет.

Возвращает

Массив(). The number of comments keyed by their status.

  • approved(число)
    The number of approved comments.

  • awaiting_moderation(число)
    The number of comments awaiting moderation (a.k.a. pending).

  • spam(число)
    The number of spam comments.

  • trash(число)
    The number of trashed comments.

  • post-trashed(число)
    The number of comments for posts that are in the trash.

  • total_comments(число)
    The total number of non-trashed comments, including spam.

  • all(число)
    The total number of pending or approved comments.

Использование

get_comment_count( $post_id );
$post_id(число)
Restrict the comment counts to the given post.
По умолчанию: 0, which indicates that comment counts for the whole site will be retrieved

Заметки

  • Global. wpdb. $wpdb WordPress database abstraction object.

Список изменений

С версии 2.0.0 Введена.

Код get_comment_count() WP 5.5.1

wp-includes/comment.php
<?php
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(
		"
		SELECT comment_approved, COUNT( * ) AS total
		FROM {$wpdb->comments}
		{$where}
		GROUP BY comment_approved
	",
		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 );
}