wp_update_comment_count_now
Updates the comment count for the post.
Description
wp_update_comment_count_now( (int) $post_id );
Parameters (1)
- 0. $post_id (int)
- The post id.
Usage
if ( !function_exists( 'wp_update_comment_count_now' ) ) { require_once ABSPATH . WPINC . '/comment.php'; } // The post id. $post_id = -1; // NOTICE! Understand what this does before running. $result = wp_update_comment_count_now($post_id);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/comment.php
- 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' );
- if ( !$post = get_post($post_id) )
- 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 $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_id, $post );
- return true;
- }