1. 首页
  2. WordPress 函数手册

wp_update_comment_count()

wp_update_comment_count( int|null $post_id, bool $do_deferred = false )

更新文章的评论计数。
Updates the comment count for post(s).

目录锚点:#说明#参数#源码


说明(Description)

当$do_deferred为false(默认为)并且评论被设置为延迟时,post_id将被添加到队列中,队列将在以后更新,并且每个post id只更新一次。如果评论没有被设置为延迟,则帖子将被更新。当$do_deferred设置为true时,所有以前延迟的post id将与当前的$post_id一起更新。另请参阅wp_update_comment_count_now():了解导致错误返回值的原因


参数(Parameters)

参数 类型 说明
$post_id (int | null) 职位ID。
$do_deferred (bool) 是否处理先前延迟的发表评论计数。

源码(Source)

/**
 * Updates the comment count for post(s).
 *
 * When $do_deferred is false (is by default) and the comments have been set to
 * be deferred, the post_id will be added to a queue, which will be updated at a
 * later date and only updated once per post ID.
 *
 * If the comments have not be set up to be deferred, then the post will be
 * updated. When $do_deferred is set to true, then all previous deferred post
 * IDs will be updated along with the current $post_id.
 *
 * @since 2.1.0
 * @see wp_update_comment_count_now() For what could cause a false return value
 *
 * @staticvar array $_deferred
 *
 * @param int $post_id Post ID
 * @param bool $do_deferred Whether to process previously deferred post comment counts
 * @return bool|void True on success, false on failure
 */
function wp_update_comment_count($post_id, $do_deferred=false) {
	static $_deferred = array();

	if ( $do_deferred ) {
		$_deferred = array_unique($_deferred);
		foreach ( $_deferred as $i => $_post_id ) {
			wp_update_comment_count_now($_post_id);
			unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */
		}
	}

	if ( wp_defer_comment_counting() ) {
		$_deferred[] = $post_id;
		return true;
	}
	elseif ( $post_id ) {
		return wp_update_comment_count_now($post_id);
	}

}
更新版本 源码位置 使用 被使用
2.1.0 wp-includes/comment.php 10 3

wp_update_comment_count() 为WP2原创文章,链接:https://www.wp2.cn/functions/wp_update_comment_count/