update_comment_cache

Updates the comment cache of given comments.

Description

update_comment_cache( (array) $comments, (bool) $update_meta_cache = true ); 

Will add the comments in $comments to the cache. If comment ID already exists in the comment cache then it will not be updated. The comment is added to the cache using the comment group with the key using the ID of the comments.

Parameters (2)

0. $comments (array)
Array of comment row objects
1. $update_meta_cache — Optional. (bool) => true
Whether to update commentmeta cache. Default true.

Usage

  1. if ( !function_exists( 'update_comment_cache' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // Array of comment row objects 
  5. $comments = array(); 
  6.  
  7. // Whether to update commentmeta cache. Default true. 
  8. $update_meta_cache = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = update_comment_cache($comments, $update_meta_cache); 
  12.  

Defined (1)

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

/wp-includes/comment.php  
  1. function update_comment_cache( $comments, $update_meta_cache = true ) { 
  2. foreach ( (array) $comments as $comment ) 
  3. wp_cache_add($comment->comment_ID, $comment, 'comment'); 
  4.  
  5. if ( $update_meta_cache ) { 
  6. // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. 
  7. $comment_ids = array(); 
  8. foreach ( $comments as $comment ) { 
  9. $comment_ids[] = $comment->comment_ID; 
  10. update_meta_cache( 'comment', $comment_ids );