wp_update_comment_count_now( int $post_id ): bool
- Since
- 2.5.0
- Source
wp-includes/comment.php:2777
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
- Scaling
- Constant
- Instructions
- 6–74
- Plugin surface
- 4 hooks
- Called by
- 1
Reaches the database via ->get_var().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 78.
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.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_delete()called directly - querycontent query
get_post()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | none |
| always | 20 | wp_cache_delete(), wp_cache_delete(), get_post() |
$new !== null | 63 | wp_cache_delete(), wp_cache_delete(), get_post(), apply_filters(), ID(), clean_post_cache(), do_action(), do_action(), do_action() |
$new === null | 74 | wp_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:
- apply_filters( pre_wp_update_comment_count_now )filterline 2806 (+29 into the body)
Filters a post's comment count before it is updated in the database.
- do_action( wp_update_comment_count )actionline 2827 (+50 into the body)
Fires immediately after a post's comment count is updated in the database.
- do_action( edit_post_{$post->post_type} )actionline 2830 (+53 into the body)
Fires once an existing post has been updated.
- do_action( edit_post )actionline 2833 (+56 into the body)
Fires once an existing post has been updated.
Uses · 5
- wp_cache_delete()Removes the cache contents matching key and group.
- get_post()Retrieves post data given a post ID or post object.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- clean_post_cache()Will clean the post in the cache.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 1
- wp_update_comment_count()Updates the comment count for post(s).
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'", $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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.