wp_update_comment_count_now

Updates the comment count for the post.

Description

wp_update_comment_count_now( (int) $post_id ); 

Parameters (1)

0. $post_id (int)
The post id.

Usage

  1. if ( !function_exists( 'wp_update_comment_count_now' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_update_comment_count_now($post_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function wp_update_comment_count_now($post_id) { 
  2. global $wpdb; 
  3. $post_id = (int) $post_id; 
  4. if ( !$post_id ) 
  5. return false; 
  6.  
  7. wp_cache_delete( 'comments-0', 'counts' ); 
  8. wp_cache_delete( "comments-{$post_id}", 'counts' ); 
  9.  
  10. if ( !$post = get_post($post_id) ) 
  11. return false; 
  12.  
  13. $old = (int) $post->comment_count; 
  14.  
  15. /** 
  16. * Filters a post's comment count before it is updated in the database. 
  17. * @since 4.5.0 
  18. * @param int $new The new comment count. Default null. 
  19. * @param int $old The old comment count. 
  20. * @param int $post_id Post ID. 
  21. */ 
  22. $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id ); 
  23.  
  24. if ( is_null( $new ) ) { 
  25. $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) ); 
  26. } else { 
  27. $new = (int) $new; 
  28.  
  29. $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) ); 
  30.  
  31. clean_post_cache( $post ); 
  32.  
  33. /** 
  34. * Fires immediately after a post's comment count is updated in the database. 
  35. * @since 2.3.0 
  36. * @param int $post_id Post ID. 
  37. * @param int $new The new comment count. 
  38. * @param int $old The old comment count. 
  39. */ 
  40. do_action( 'wp_update_comment_count', $post_id, $new, $old ); 
  41. /** This action is documented in wp-includes/post.php */ 
  42. do_action( 'edit_post', $post_id, $post ); 
  43.  
  44. return true;