function wp_update_comment_count_now($post_id) {}

Updates the comment count for the post.


  • int $post_id: Post ID

Return values

returns:False on ‘0’ $post_id or if post with ID does not exist. True on success.

Defined actions

  • wp_update_comment_count
    do_action('wp_update_comment_count', $post_id, $new, $old);
  • edit_post
    do_action('edit_post', $post_id, $post);

Source code

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

	if ( 'page' == $post->post_type )

		clean_page_cache( $post_id );


		clean_post_cache( $post_id );

	do_action('wp_update_comment_count', $post_id, $new, $old);

	do_action('edit_post', $post_id, $post);

	return true;



No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: