Hi -
We need help writing a cron job in WordPress that will (nightly) auto-expire events that have completed (passed their end date or final recurrence date) in Modern Tribe's Event Calendar Pro plugin. It should set these custom post types to drafts after querying custom fields for end dates/times/recurrences.
Thanks
Marcus
John Cotton answers:
What do you need to know?
John Cotton comments:
I don't know the plugin, but this - with a couple of tweaks to edit field names - will do it.
function schedule_auto_expire_events(){
// Check we're scheduled
if( !wp_next_scheduled( 'auto_expire_events' ) ) {
wp_schedule_event( time(), 'daily', 'auto_expire_events' );
}
}
add_action( 'init', 'schedule_auto_expire_events' );
// Hook to our cron
function auto_expire_events(){
// This needs tweaking to match the extact post type
$query = new WP_Query( array( 'posts_per_page' => -1, 'post_status' => array( 'pending', 'publish', 'future' ), 'post_type' => 'event' ) );
while ( $query->have_posts() ) {
$query->the_post();
$id = get_the_ID();
$expire = get_post_meta( $id, '_the_meta_key', true ); // You need to edit this to add the name of the key you want to test
if( strtotime($expire) < time() ) {
wp_update_post( array( 'ID' => $id, 'post_status' => 'draft' ) );
}
}
}
add_action( 'auto_expire_events', 'auto_expire_events' );
Kyle answers:
I have a perfect function, give me a few minutes
Kyle comments:
This should work, it checks the past week, you could reduce that if you want.
add_action('my_daily_event', 'do_this_daily');
function my_activation() {
if ( !wp_next_scheduled( 'my_daily_event' ) ) {
wp_schedule_event( time(), 'daily', 'my_daily_event');
}
}
add_action('wp', 'my_activation');
function do_this_daily() {
$current_date = date('j M Y');
$past = date("j M Y",strtotime("-1 week"));
$find_events = new tribe_get_events(array(
'posts_per_page' => -1,
'post_status' => 'publish',
'start_date' => '$past',
'end_date'=> $current_date,
));
if($find_orders->have_posts()) : while( $find_orders->have_posts() ) : $find_orders->the_post();
global $post;
$my_post = array(
'ID' => $post->ID,
'post_status' => 'draft'
);
wp_update_post( $my_post );
endwhile;
endif;
}
Kyle comments:
Sorry let me revise
add_action('my_daily_event', 'do_this_daily');
function my_activation() {
if ( !wp_next_scheduled( 'my_daily_event' ) ) {
wp_schedule_event( time(), 'daily', 'my_daily_event');
}
}
add_action('wp', 'my_activation');
function do_this_daily() {
$current_date = date('j M Y');
$past = date("j M Y",strtotime("-1 week"));
$find_events = tribe_get_events(array(
'posts_per_page' => -1,
'post_status' => 'publish',
'start_date' => '$past',
'end_date'=> $current_date,
));
foreach( $find_events as $events){
global $post;
$my_post = array(
'ID' => $post->ID,
'post_status' => 'draft'
);
wp_update_post( $my_post );
}
Kyle comments:
Sorry, still early here, this should work http://pastie.org/9148896
Kyle comments:
If you wanted to execute it at a certain time of day, change the time() part to the UTC equivalent
Firoja_Imtosupport answers:
Hi,
First thing is you install Crony Cronjob Manager login via wp-admin
Click on Manage cronjob and You can add a new,
We can write all the code you want under Custom PHP
Thanks