On this page
wp_update_comment_count_now( int $post_id ): bool
Updates the comment count for the post.
Parameters
$post_idint Required-
Post ID
Return
bool True on success, false if the post does not exist.
Source
File: wp-includes/comment.php. View all references
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;
}
Hooks
- do_action( 'edit_post',
int $post_ID ,WP_Post $post ) -
Fires once an existing post has been updated.
- do_action( "edit_post_{$post->post_type}",
int $post_ID ,WP_Post $post ) -
Fires once an existing post has been updated.
- apply_filters( 'pre_wp_update_comment_count_now',
int|null $new ,int $old ,int $post_id ) -
Filters a post’s comment count before it is updated in the database.
- do_action( 'wp_update_comment_count',
int $post_id ,int $new ,int $old ) -
Fires immediately after a post’s comment count is updated in the database.
Related
Uses
| Uses | Description |
|---|---|
| wp_cache_delete() wp-includes/cache.php | Removes the cache contents matching key and group. |
| clean_post_cache() wp-includes/post.php | Will clean the post in the cache. |
| wpdb::update() wp-includes/class-wpdb.php | Updates a row in the table. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| wpdb::get_var() wp-includes/class-wpdb.php | Retrieves one variable from the database. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| wp_update_comment_count() wp-includes/comment.php | Updates the comment count for post(s). |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_update_comment_count_now