Skip to content

Instantly share code, notes, and snippets.

@norcross
Created December 18, 2012 23:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save norcross/4333071 to your computer and use it in GitHub Desktop.
Save norcross/4333071 to your computer and use it in GitHub Desktop.
set cron to delete post revisions
<?php
// add filter to allow for new weekly schedule
add_filter( 'cron_schedules', 'rkv_weekly_cron' );
function rkv_weekly_cron( $schedules ) {
// Adds once weekly to the existing schedules.
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly' )
);
return $schedules;
}
add_action('rkv_revision_cron', 'rkv_revision_delete');
// set cron schedule
function rkv_revision_schedule() {
if ( !wp_next_scheduled( 'rkv_revision_cron' ) ) {
wp_schedule_event( time(), 'weekly', 'rkv_revision_cron');
}
}
add_action('wp', 'rkv_revision_schedule');
// run deletion on revisions
function rkv_revision_delete() {
// query revisions and get array of IDs
$args = array(
'fields' => 'ids',
'post_type' => 'revision',
'numberposts' => -1
);
$revisions = get_posts($args);
// loop through and delete each
foreach ($revisions as $revision):
wp_delete_post( $revision );
endforeach;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment