wppaste
WordPress

wp_update_comment_count_now( int $post_id ): bool

Since
2.5.0
Source
wp-includes/comment.php:3106
Updates the comment count for the post.

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_idint
Post ID

Return value

bool
True on success, false if the post does not exist.

Performance profile

How much work a call to wp_update_comment_count_now() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_var().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–74

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 78.

Plugin surface
4 hooks

Third-party callbacks on 'pre_wp_update_comment_count_now', 'wp_update_comment_count', 'edit_post_{$post->post_type}' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_delete()called directly
  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->get_var()called directly

Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_update_comment_count_now() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always6none
always20wp_cache_delete(), wp_cache_delete(), get_post()
$new !== null63wp_cache_delete(), wp_cache_delete(), get_post(), apply_filters(), ID(), clean_post_cache(), do_action(), do_action(), do_action()
$new === null74wp_cache_delete(), wp_cache_delete(), get_post(), apply_filters(), ->prepare(), ->get_var(), ID(), clean_post_cache(), do_action(), do_action(), do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 78 instructions, 6–74 executed per call, 3 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while wp_update_comment_count_now() runs, in this order:

  1. apply_filters( pre_wp_update_comment_count_now )filterline 3135 (+29 into the body)

    Filters a post's comment count before it is updated in the database.

  2. do_action( wp_update_comment_count )actionline 3156 (+50 into the body)

    Fires immediately after a post's comment count is updated in the database.

  3. do_action( edit_post_{$post->post_type} )actionline 3159 (+53 into the body)

    Fires once an existing post has been updated.

  4. do_action( edit_post )actionline 3162 (+56 into the body)

    Fires once an existing post has been updated.

Uses · 5

Used by · 1

Source code

function wp_update_comment_count_now( $post_id ) {	global $wpdb; 	$post_id = (int) $post_id; 	if ( ! $post_id ) {		return false;	} 	wp_cache_delete( 'comments-0', 'counts' );	wp_cache_delete( "comments-{$post_id}", 'counts' ); 	$post = get_post( $post_id ); 	if ( ! $post ) {		return false;	} 	$old = (int) $post->comment_count; 	/**	 * Filters a post's comment count before it is updated in the database.	 *	 * @since 4.5.0	 *	 * @param int|null $new     The new comment count. Default null.	 * @param int      $old     The old comment count.	 * @param int      $post_id Post ID.	 */	$new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id ); 	if ( is_null( $new ) ) {		$new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note'", $post_id ) );	} else {		$new = (int) $new;	} 	$wpdb->update( $wpdb->posts, array( 'comment_count' => $new ), array( 'ID' => $post_id ) ); 	clean_post_cache( $post ); 	/**	 * Fires immediately after a post's comment count is updated in the database.	 *	 * @since 2.3.0	 *	 * @param int $post_id Post ID.	 * @param int $new     The new comment count.	 * @param int $old     The old comment count.	 */	do_action( 'wp_update_comment_count', $post_id, $new, $old ); 	/** This action is documented in wp-includes/post.php */	do_action( "edit_post_{$post->post_type}", $post_id, $post ); 	/** This action is documented in wp-includes/post.php */	do_action( 'edit_post', $post_id, $post ); 	return true;}

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.