lesterchan / wp-sweep

WP-Sweep allows you to clean up unused, orphaned and duplicated data in your WordPress. It also optimizes your database tables.
https://wordpress.org/plugins/wp-sweep/
152 stars 23 forks source link

Feature request Delete revisions older than XX day(s) #52

Closed colomet closed 6 years ago

colomet commented 6 years ago

Some times we can not delete revisions as some of them could be useful. But for sure, 1 year old revisions are not longer need it. Maybe a good way to decrease database and to keep important work is to be able to delete just older revisions.

thanks

lesterchan commented 6 years ago

You can use this code for that.

global $wpdb;
$limit = 1000;

$time_format = get_option( 'time_format' );
$date_format = get_option( 'date_format' );

$start = time();
// We keep only 1 week worth of post revisions.
$revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE (post_type = 'revision' OR post_status = 'auto-draft') AND post_date_gmt < DATE_SUB(NOW(), INTERVAL 7 DAY) LIMIT %d", $limit ) );
foreach ( $revision_ids as $revision_id ) {
    wp_delete_post_revision( $revision_id );
}
echo '<p>' . esc_html( date( $time_format . ' ' . $date_format ) ) . ': ' . count( $revision_ids ) . ' revisions deleted. Took ' . esc_html( time() - $start ) . 's.</p>';