返回介绍

get_comment_count()

发布于 2017-09-10 23:05:46 字数 2753 浏览 930 评论 0 收藏 0

get_comment_count( int $post_id )

The amount of comments in a post or total comments.


description

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.


参数

$post_id

(int) (Optional) Comment amount in post if > 0, else total comments blog wide.


返回值

(array) The amount of spam, approved, awaiting moderation, and total comments.


源代码

File: wp-includes/comment.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 $comment_count;
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/wp-db.php: wpdb::get_results()
  • wp-includes/wp-db.php: wpdb::prepare()

Used By

  • wp-includes/comment.php: wp_count_comments()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文