Title: update_comment_cache
Published: April 25, 2014
Last modified: August 20, 2026

---

# update_comment_cache( WP_Comment[] $comments, bool $update_meta_cache = true )

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#wp--skip-link--target)

Updates the comment cache of given comments.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#description)󠁿

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](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#parameters)󠁿

 `$comments`[WP_Comment](https://developer.wordpress.org/reference/classes/wp_comment/)[]
required

Array of comment objects

`$update_meta_cache`booloptional

Whether to update commentmeta cache.

Default:`true`

## 󠀁[Source](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#source)󠁿

    ```php
    function update_comment_cache( $comments, $update_meta_cache = true ) {
    	$data = array();
    	foreach ( (array) $comments as $comment ) {
    		$data[ $comment->comment_ID ] = $comment;
    	}
    	wp_cache_add_multiple( $data, 'comment' );

    	if ( $update_meta_cache ) {
    		// Avoid `wp_list_pluck()` in case `$comments` is passed by reference.
    		$comment_ids = array();
    		foreach ( $comments as $comment ) {
    			$comment_ids[] = $comment->comment_ID;
    		}
    		update_meta_cache( 'comment', $comment_ids );
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/comment.php#L3782)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/comment.php#L3782-L3797)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_cache_add_multiple()](https://developer.wordpress.org/reference/functions/wp_cache_add_multiple/)`wp-includes/cache.php` |

Adds multiple values to the cache in one call.

  | 
| [update_meta_cache()](https://developer.wordpress.org/reference/functions/update_meta_cache/)`wp-includes/meta.php` |

Updates the metadata cache for the specified objects.

  |

| Used by | Description | 
| [_prime_comment_caches()](https://developer.wordpress.org/reference/functions/_prime_comment_caches/)`wp-includes/comment.php` |

Adds any comments from the given IDs to the cache that do not already exist in cache.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/update_comment_cache/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Introduced the `$update_meta_cache` parameter. | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fupdate_comment_cache%2F)
before being able to contribute a note or feedback.