Viewing 6 replies - 1 through 6 (of 6 total)
  • You’ll need to add

    global EM_Event;

    to the first line of actually_delete_past_events so that the object can be accessed.

    I recommend making a backup of your database before running a mass delete operation like this, just in case things don’t go quite as planned.

    Jeff S,
    please share how did you go with deleting old events and what was the final code you used. I’m keen to also delete all past events to clean up the database.

    Plugin Support angelo_nwl

    (@angelo_nwl)

    it’s not possible out the box at the moment. it may be possible with custom coding but unfortunately that’s not something we can help with.

    Thread Starter Jeff C

    (@12steprecovery)

    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?

    Thread Starter Jeff C

    (@12steprecovery)

    Finally I gave up on EM_Events::delete() and used a foreach loop and moved the posts to trash rather than deleting them (a bit safer in case I made a mistake somewhere). I also made the schedule hourly as it’s deleting events over 2 days old – and there are a lot stored up in our database – and I’m assuming it’s timing out.

    function set_up_scheduled_event() {
        if ( !wp_next_scheduled( 'Delete_Past_Events' ) ) {
            wp_schedule_event( time(), 'hourly', '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)); // a way of checking the events that are going to be deleted
    foreach ($past_events as $event) {
    if ($event->recurrence_id) wp_trash_post($event->post_id); // only delete if it's been produced by a recurrent event
    }
    }

    This feature looks really cool!
    Anyone know how to expand it to delete the associated event image from the media library also?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Deleting past events automatically’ is closed to new replies.