Thanks for the input. It’s taken me a while to get to do further work on this.
There were some errors in the code above and so the code I’ve just tried is as follows:
function set_up_scheduled_event() {
if ( !wp_next_scheduled( 'Delete_Past_Events' ) ) {
wp_schedule_event( time(), 'daily', 'Delete_Past_Events');
}
}
add_action('wp', 'set_up_scheduled_event');
add_action('Delete_Past_Events', 'actually_delete_past_events');
function actually_delete_past_events() {
global $EM_Event;
$past_events = EM_Events::get(array('scope'=>'before-yesterday'));
file_put_contents(get_stylesheet_directory().'/print_array.txt', print_r($past_events, true));
EM_Events::delete($past_events);
}
with the scope provided by the following code:
add_filter( 'em_events_build_sql_conditions', 'my_em_scope_conditions',1,2);
function my_em_scope_conditions($conditions, $args){
if( !empty($args['scope']) && $args['scope']=='this-week' ){
$start_date = date('Y-m-d', strtotime('Today', time()));
$end_date = date('Y-m-d', strtotime('Today +6 day', time()));
$conditions['scope'] = " (event_start_date BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE)) OR (event_end_date BETWEEN CAST('$end_date' AS DATE) AND CAST('$start_date' AS DATE))";
}
if( !empty($args['scope']) && $args['scope']=='next-week' ){
$start_date = date('Y-m-d',strtotime("Today +7 day", time()));
$end_date = date('Y-m-d',strtotime("Today +13 day", time()));
$conditions['scope'] = " (event_start_date BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE)) OR (event_end_date BETWEEN CAST('$end_date' AS DATE) AND CAST('$start_date' AS DATE))";
}
if( !empty($args['scope']) && $args['scope']=='before-yesterday' ){
$start_date = date('Y-m-d',strtotime("5 years ago", time()));
$end_date = date('Y-m-d',strtotime("2 days ago", time()));
$conditions['scope'] = " (event_start_date BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE)) OR (event_end_date BETWEEN CAST('$end_date' AS DATE) AND CAST('$start_date' AS DATE))";
}
return $conditions;
}
add_filter( 'em_get_scopes','my_em_scopes',1,1);
function my_em_scopes($scopes){
$my_scopes = array(
'this-week' => 'This Week',
'next-week' => 'Next Week',
'before-yesterday' => 'Before Yesterday'
);
array_splice($scopes, 5, 0, $my_scopes);
return $scopes;
}
However it didn’t delete any events when I tested it – the array is correctly created and printed to a file, so I can see what should be deleted, but EM_Events::delete($past_events) doesn’t seem to work.
Any ideas?