WordPress.org

Make WordPress Core

Changeset 36318


Ignore:
Timestamp:
01/15/2016 01:41:35 PM (4 years ago)
Author:
swissspidy
Message:

Comments: Add a new pre_wp_update_comment_count_now filter.

This allows filtering a post's comment count before it is queried and updated in the database.

Props peterwilsoncc for initial patch.
Fixes #35060.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r36272 r36318  
    20952095
    20962096    $old = (int) $post->comment_count;
    2097     $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
     2097
     2098    /**
     2099     * Filters a post's comment count before it is updated in the database.
     2100     *
     2101     * @since 4.5.0
     2102     *
     2103     * @param int $new     The new comment count. Default null.
     2104     * @param int $old     The old comment count.
     2105     * @param int $post_id Post ID.
     2106     */
     2107    $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );
     2108
     2109    if ( is_null( $new ) ) {
     2110        $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) );
     2111    } else {
     2112        $new = (int) $new;
     2113    }
     2114
    20982115    $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
    20992116
Note: See TracChangeset for help on using the changeset viewer.