update_comment_cache() WordPress Function

The update_comment_cache() function is used to update the cache of comments for a given post. This function is typically called after a new comment is added to a post.

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

Updates the comment cache of given comments.


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.


Top ↑

Parameters

$comments

(WP_Comment[])(Required)Array of comment objects

$update_meta_cache

(bool)(Optional)Whether to update commentmeta cache.

Default value: true


Top ↑

Source

File: wp-includes/comment.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 );
	}
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced the $update_meta_cache parameter.
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More