Last active
February 14, 2018 18:51
-
-
Save misfist/688baaadd9685a2539f9a7a72867301e to your computer and use it in GitHub Desktop.
Cron to Delete Revisions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// add filter to allow for new weekly schedule | |
add_filter( 'cron_schedules', 'my_weekly_cron' ); | |
function my_weekly_cron( $schedules ) { | |
// Adds once weekly to the existing schedules. | |
$schedules['weekly'] = array( | |
'interval' => 604800, | |
'display' => __( 'Once Weekly' ) | |
); | |
return $schedules; | |
} | |
add_action('my_revision_cron', 'my_revision_delete'); | |
// set cron schedule | |
function my_revision_schedule() { | |
if ( !wp_next_scheduled( 'my_revision_cron' ) ) { | |
wp_schedule_event( time(), 'weekly', 'my_revision_cron'); | |
} | |
} | |
add_action('wp', 'my_revision_schedule'); | |
// run deletion on revisions | |
function my_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