Skip to content

Instantly share code, notes, and snippets.

Created October 4, 2011 18:40
Show Gist options
  • Save anonymous/1262429 to your computer and use it in GitHub Desktop.
Save anonymous/1262429 to your computer and use it in GitHub Desktop.
checks current time against time of post and expires the post if it's older than today.
// setting posts with current date or older to draft
if (!wp_next_scheduled('sfn_expire_hook')){
wp_schedule_event( time(), 'hourly', 'sfn_expire_hook');
}
add_action( 'sfn_expire_hook', 'sfn_show_expire');
add_action( 'sfn_expire_hook', 'sfn_show_expire');
function sfn_show_expire(){
global $wpdb;
$server_time = date('U');
// below echoes date so we know what's up with the server date
// echo date( 'Y, m, d - g:i' );
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'show' AND post_status = 'publish'");
if( !empty($result)) foreach ($result as $a){
// getting the show time
$show_time = get_the_time('U', $a->ID);
// adding 3 hours to the post time so things don't expire right away
$show_time_three = strtotime( '+3 hours', $show_time );
// if the show time is more the 3 hours older than the server time
// lets set the show to draft since it's old
if ( $server_time > $show_time_three ){
$my_post = array();
$my_post['ID'] = $a->ID;
$my_post['post_status'] = 'draft';
wp_update_post( $my_post );
}
} // end foreach
}
// this simply serves to run the above function for testing don't do this live b/c it will run every time the site loads and that's a bad thing
//add_action( 'init', 'sfn_show_expire' );
//
// need to uncomment the clearing hook below if you're changing the cron settings
// wp_clear_scheduled_hook( 'sfn_expire_hook' );
//
// the code below tells you when the hook is sheduled
// $crontest = wp_get_schedule( 'sfn_expire_hook' );
// echo $crontest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment