函数文档

wp_count_comments()

💡 云策文档标注

概述

wp_count_comments() 函数用于获取整个站点或单个文章的评论统计数量,支持缓存机制以提高性能。返回一个包含按状态分类的评论计数的对象。

关键要点

  • 函数可接受一个可选的 $post_id 参数,默认为 0 表示获取整个站点的评论计数。
  • 返回一个 stdClass 对象,包含 approved、moderated、spam、trash、post-trashed、total_comments 和 all 等属性。
  • 内部使用 wp_cache_get() 和 wp_cache_set() 进行缓存,避免重复查询数据库。
  • 通过 apply_filters('wp_count_comments', $count, $post_id) 钩子允许开发者过滤评论计数。
  • 与 get_comment_count() 函数相关,后者用于获取实时评论计数。

代码示例

// 获取整个站点的评论计数
$comments_count = wp_count_comments();
echo "Comments in moderation: " . $comments_count->moderated . "";
echo "Comments approved: " . $comments_count->approved . "";
echo "Comments in Spam: " . $comments_count->spam . "";
echo "Comments in Trash: " . $comments_count->trash . "";
echo "Total Comments: " . $comments_count->total_comments . "";

// 获取特定文章的评论计数
$comments_count = wp_count_comments(123);
echo "Comments in moderation: " . $comments_count->moderated . "";
echo "Comments approved: " . $comments_count->approved . "";
echo "Comments in Spam: " . $comments_count->spam . "";
echo "Comments in Trash: " . $comments_count->trash . "";
echo "Total Comments: " . $comments_count->total_comments . "";

注意事项

  • 缓存机制可能影响实时性,如需最新数据可考虑使用 get_comment_count()。
  • 返回对象中的 moderated 属性对应 awaiting_moderation 状态。
  • total_comments 属性不包括已删除的评论,而 all 属性包括待审核和已批准的评论。

📄 原文内容

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

Description

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

See also

Parameters

$post_idintoptional
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.

  • approved int
    The number of approved comments.
  • moderated int
    The number of comments awaiting moderation (a.k.a. pending).
  • spam int
    The number of spam comments.
  • trash int
    The number of trashed comments.
  • post-trashed int
    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.

Source

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

Hooks

apply_filters( ‘wp_count_comments’, array|stdClass $count, int $post_id )

Filters the comments count for a given post or the whole site.

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Retrieve comment count for a post

    ";
    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 4 content

    Default usage
    Retrieve comment count for a site.

    ";
    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 />";
    ?>