wp_update_comment_count_now()更新文章的评论总数

目录

描述

Updates the comment count for the post. It is used by wp_update_comment_count().

wp_update_comment_count_now() 描述

用法

<?php wp_update_comment_count_now$post_id ); ?>

wp_update_comment_count_now() 用法

参数

$post_id

(integer) (必填) The post ID

默认值: None

wp_update_comment_count_now() 参数

返回值

(boolean

False if $post_id is not valid. True on success.

wp_update_comment_count_now() 返回值

注意

wp_update_comment_count_now() 注意

历史

添加于 版本: 2.5.0

wp_update_comment_count_now() 历史

源文件

wp_update_comment_count_now() 函数的代码位于 wp-includes/comment.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
function wp_update_comment_count_now($post_id) {
	global $wpdb;
	$post_id = (int) $post_id;
	if ( !$post_id )
		return false;
	if ( !$post = get_post($post_id) )
		return false;

	$old = (int) $post->comment_count;
	$new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
	$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;
}

//
// Ping and trackback functions.
//

wp_update_comment_count_now() 源文件