clean_comment_cache example

// Cache current status for each comment.     $statuses = array();
    foreach ( $comments as $comment ) {
        $statuses[ $comment->comment_ID ] = $comment->comment_approved;
    }
    add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses );

    // Set status for all comments to post-trashed.     $result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) );

    clean_comment_cache( array_keys( $statuses ) );

    /** * Fires after comments are sent to the Trash. * * @since 2.9.0 * * @param int $post_id Post ID. * @param array $statuses Array of comment statuses. */
    do_action( 'trashed_post_comments', $post_id$statuses );

    

    do_action( 'delete_comment', $comment->comment_ID, $comment );

    // Move children up a level.     $children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) );
    if ( ! empty( $children ) ) {
        $wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) );
        clean_comment_cache( $children );
    }

    // Delete metadata.     $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
    foreach ( $meta_ids as $mid ) {
        delete_metadata_by_mid( 'comment', $mid );
    }

    if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) {
        return false;
    }

    
// Prepared as strings since comment_id is an unsigned BIGINT, and using %d will constrain the value to the maximum signed BIGINT.             $format_string = implode( ', ', array_fill( 0, is_countable( $comment_ids ) ? count( $comment_ids ) : 0, '%s' ) );

            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->comments} WHERE comment_id IN ( " . $format_string . " )", $comment_ids ) );
            $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( " . $format_string . " )", $comment_ids ) );

            foreach ( $comment_ids as $comment_id ) {
                do_action( 'deleted_comment', $comment_id$comments[ $comment_id ] );
                unset( $comments[ $comment_id ] );
            }

            clean_comment_cache( $comment_ids );
            do_action( 'akismet_delete_comment_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 );
        }

        if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11)$wpdb->comments ) ) // lucky number             $wpdb->query("OPTIMIZE TABLE {$wpdb->comments}");
    }

    public static function delete_old_comments_meta() {
        global $wpdb;

        $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 );

        
Home | Imprint | This part of the site doesn't use cookies.