返回介绍

wp_update_comment_count_now()

发布于 2017-09-11 13:03:32 字数 3046 浏览 1047 评论 0 收藏 0

wp_update_comment_count_now( int $post_id )

Updates the comment count for the post.


description


参数

$post_id

(int) (Required) Post ID


返回值

(bool) True on success, false on '0' $post_id or if post with ID does not exist.


源代码

File: wp-includes/comment.php

function wp_update_comment_count_now($post_id) {
	global $wpdb;
	$post_id = (int) $post_id;
	if ( !$post_id )
		return false;

	wp_cache_delete( 'comments-0', 'counts' );
	wp_cache_delete( "comments-{$post_id}", 'counts' );

	if ( !$post = get_post($post_id) )
		return false;

	$old = (int) $post->comment_count;

	/**
	 * Filters a post's comment count before it is updated in the database.
	 *
	 * @since 4.5.0
	 *
	 * @param int $new     The new comment count. Default null.
	 * @param int $old     The old comment count.
	 * @param int $post_id Post ID.
	 */
	$new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );

	if ( is_null( $new ) ) {
		$new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) );
	} else {
		$new = (int) $new;
	}

	$wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );

	clean_post_cache( $post );

	/**
	 * Fires immediately after a post's comment count is updated in the database.
	 *
	 * @since 2.3.0
	 *
	 * @param int $post_id Post ID.
	 * @param int $new     The new comment count.
	 * @param int $old     The old comment count.
	 */
	do_action( 'wp_update_comment_count', $post_id, $new, $old );
	/** This action is documented in wp-includes/post.php */
	do_action( 'edit_post', $post_id, $post );

	return true;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/comment.php: pre_wp_update_comment_count_now
  • wp-includes/cache.php: wp_cache_delete()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: do_action()
  • wp-includes/post.php: clean_post_cache()
  • wp-includes/post.php: edit_post
  • wp-includes/post.php: get_post()
  • wp-includes/wp-db.php: wpdb::get_var()
  • wp-includes/wp-db.php: wpdb::update()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/comment.php: wp_update_comment_count
  • Show 6 more uses Hide more uses

Used By

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

User Contributed Notes

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

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

发布评论

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