Scheduled Post Revisions Purge
Last updated 1 month ago
PHP
function delete_old_post_revisions() {
global $wpdb;
// Delete post revisions older than 3 months.
$wpdb->query("
DELETE FROM {$wpdb->posts}
WHERE post_type = 'revision'
AND post_date < NOW() - INTERVAL 3 MONTH
");
}
// Schedule the event if it’s not already scheduled.
if (!wp_next_scheduled('delete_old_post_revisions_hook')) {
wp_schedule_event(time(), 'weekly', 'delete_old_post_revisions_hook');
}
add_action('delete_old_post_revisions_hook', 'delete_old_post_revisions');
// Clear the event on plugin/theme deactivation.
register_deactivation_hook(__FILE__, function () {
wp_clear_scheduled_hook('delete_old_post_revisions_hook');
});
All code snippets are licensed GPLv2 or later unless otherwise stated.