wp_update_comment_count_now

The timeline below displays how wordpress function wp_update_comment_count_now has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.3

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Post ID
 * @return bool True on success, false if the post does not exist.
 */
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;
}

WordPress Version: 5.1

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
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->post_type}", $post_id, $post);
    /** This action is documented in wp-includes/post.php */
    do_action('edit_post', $post_id, $post);
    return true;
}

WordPress Version: 4.5

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
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;
}

WordPress Version: 4.4

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
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;
    $new = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id));
    $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;
}

WordPress Version: 4.1

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
function wp_update_comment_count_now($post_id)
{
    global $wpdb;
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (!$post = get_post($post_id)) {
        return false;
    }
    $old = (int) $post->comment_count;
    $new = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id));
    $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;
}

WordPress Version: 3.9

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 * @uses $wpdb
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
function wp_update_comment_count_now($post_id)
{
    global $wpdb;
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (!$post = get_post($post_id)) {
        return false;
    }
    $old = (int) $post->comment_count;
    $new = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id));
    $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;
}

WordPress Version: 3.8

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 * @uses $wpdb
 * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
 * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
function wp_update_comment_count_now($post_id)
{
    global $wpdb;
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (!$post = get_post($post_id)) {
        return false;
    }
    $old = (int) $post->comment_count;
    $new = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id));
    $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;
}

WordPress Version: 3.7

/**
 * Updates the comment count for the post.
 *
 * @since 2.5.0
 * @uses $wpdb
 * @uses do_action() Calls 'wp_update_comment_count' hook on $post_id, $new, and $old
 * @uses do_action() Calls 'edit_posts' hook on $post_id and $post
 *
 * @param int $post_id Post ID
 * @return bool True on success, false on '0' $post_id or if post with ID does not exist.
 */
function wp_update_comment_count_now($post_id)
{
    global $wpdb;
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (!$post = get_post($post_id)) {
        return false;
    }
    $old = (int) $post->comment_count;
    $new = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id));
    $wpdb->update($wpdb->posts, array('comment_count' => $new), array('ID' => $post_id));
    clean_post_cache($post);
    do_action('wp_update_comment_count', $post_id, $new, $old);
    do_action('edit_post', $post_id, $post);
    return true;
}