wp_update_comment_count

Updates the comment count for post(s).

Description

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

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.

Returns (bool|void)

True on success, false on failure or if post with ID does not exist.

Parameters (2)

0. $post_id (int|null)
The post id.
1. $do_deferred — Optional. (constant) => false
Whether to process previously deferred post comment counts. Default false.

Usage

  1. if ( !function_exists( 'wp_update_comment_count' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // The post id. 
  5. $post_id = null; 
  6.  
  7. // Optional. Whether to process previously deferred 
  8. // post comment counts. Default false. 
  9. $do_deferred = false; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = wp_update_comment_count($post_id, $do_deferred); 
  13.  

Defined (1)

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

/wp-includes/comment.php  
  1. function wp_update_comment_count($post_id, $do_deferred=false) { 
  2. static $_deferred = array(); 
  3.  
  4. if ( empty( $post_id ) && ! $do_deferred ) { 
  5. return false; 
  6.  
  7. if ( $do_deferred ) { 
  8. $_deferred = array_unique($_deferred); 
  9. foreach ( $_deferred as $i => $_post_id ) { 
  10. unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */ 
  11.  
  12. $_deferred[] = $post_id; 
  13. return true; 
  14. elseif ( $post_id ) { 
  15. return wp_update_comment_count_now($post_id); 
  16.