wp_update_comment_count() WordPress Function

wp_update_comment_count() is a function used to update the comment count for a post or page. This function can be used to manually update the comment count, or to automatically update the comment count when a comment is added or deleted.

wp_update_comment_count( int|null $post_id, bool $do_deferred = false ) #

Updates the comment count for post(s).


Description

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.

Top ↑

See also


Top ↑

Parameters

$post_id

(int|null)(Required)Post ID.

$do_deferred

(bool)(Optional) Whether to process previously deferred post comment counts.

Default value: false


Top ↑

Return

(bool|void) True on success, false on failure or if post with ID does not exist.


Top ↑

Source

File: wp-includes/comment.php

function wp_update_comment_count( $post_id, $do_deferred = false ) {
	static $_deferred = array();

	if ( empty( $post_id ) && ! $do_deferred ) {
		return false;
	}

	if ( $do_deferred ) {
		$_deferred = array_unique( $_deferred );
		foreach ( $_deferred as $i => $_post_id ) {
			wp_update_comment_count_now( $_post_id );
			unset( $_deferred[ $i ] );
			/** @todo Move this outside of the foreach and reset $_deferred to an array instead */
		}
	}

	if ( wp_defer_comment_counting() ) {
		$_deferred[] = $post_id;
		return true;
	} elseif ( $post_id ) {
		return wp_update_comment_count_now( $post_id );
	}

}


Top ↑

Changelog

Changelog
VersionDescription
2.1.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