• Resolved randmanrjr

    (@randmanrjr)


    I am trying to implement some code to return some past events and display them in a page template for review before running a mass delete function.

    The eventual plan is to do something similar to what is described here. I would like to schedule a function to remove past events either once an hour or once a day.

    
    $monthago = date('Y-m-d', strtotime("-1 month"));
    $yesterday = date( 'Y-m-d' , strtotime("yesterday"));
    global $EM_Event;
    $past_events = EM_Events::get(array(
        'scope'=> $monthago . ',' . $yesterday,
         'limit'=> 100
    ));
            ?>
            <pre>
                <?php echo count($past_events) . " events" . "\n"; ?>
                <?php echo print_r($past_events, true); ?>
            </pre>
    

    When I load the page with this code, it returns an array of past events, some of which have an empty ‘post_id’

    When I look up the event in the database by event_id, I can see that the post in fact has a post_id.

    Further, when I modify the args passed to EM_events::get() to include the ‘post_id’ it returns the correct event, but the ‘post_id’ is still blank.

    I’ve tried this on both php versions 5.4 and 5.6 with the same results on the same server.

    Is there something I’m missing?

    Thanks,

    Randy

    • This topic was modified 7 years, 6 months ago by randmanrjr.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter randmanrjr

    (@randmanrjr)

    Here is the top part of the output for the event when retrieved by EM_Events::get() using the ‘post_id’ for a specific event.

    1 events
    Array
    (
    [0] => EM_Event Object
    (
    [event_id] => 58590
    [post_id] =>
    [event_slug] => circuit-training-lauren-2-2016-11-16
    [event_owner] => 6
    [event_name] => Circuit Training with Kellie
    [event_start_time] => 05:30:00
    [event_end_time] => 06:00:00
    [event_all_day] => 0
    [event_start_date] => 2016-11-16
    [event_end_date] => 2016-11-16
    [post_content] => No Registration Required!
    [event_rsvp] => 0
    [event_rsvp_date] => 2016-11-16
    [event_rsvp_time] => 00:00:00
    [event_rsvp_spaces] =>
    [event_spaces] => 0
    [event_private] => 0
    [location_id] => 29
    [recurrence_id] => 15188
    [event_status] => 1
    [blog_id] =>
    [group_id] => 0
    [event_attributes] => N;
    [recurrence] => 0
    [recurrence_interval] => 1
    [recurrence_freq] => weekly
    [recurrence_byday] => 3
    [recurrence_days] => 0
    [recurrence_byweekno] => 1
    [recurrence_rsvp_days] =>
    [event_owner_anonymous] =>
    [event_owner_name] =>
    [event_owner_email] =>

    Thread Starter randmanrjr

    (@randmanrjr)

    I manually removed all of past events from the wp-admin dashboard. Now when running this script, I am getting the newly past events, and they all have post_id’s

    Not sure what the hiccup was.

    Thread Starter randmanrjr

    (@randmanrjr)

    Here is my final code to automatically delete past recurring events.

    The job is scheduled hourly to ensure that the number of past events is not too large.

    It deletes past events that are older than two days old out to a week.
    If it is run on schedule there really should not be any accumulation beyond 2 days old.

    I’ve also found it wise to set a limit on the number of events processed at any one time.

    I chose to have the events force deleted vs trashed for a more automated system.

    
    //automatically remove past recurring events from event manager
    
    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;
        $weekago                = date('Y-m-d', strtotime("-7 day"));
        $day_b_for_yesterday    = date( 'Y-m-d' , strtotime("-2 day"));
        $past_events            = EM_Events::get(array(
            'scope'     => $weekago . ',' . $day_b_for_yesterday,
            'limit'     => 100
        ));
        foreach ($past_events as $event) {
            if ($event->recurrence_id) wp_delete_post($event->post_id, true); // only delete if it's been produced by a recurrent event
        }
    }
    
    • This reply was modified 7 years, 6 months ago by randmanrjr.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘EM_events::get() returns events with blank post_id’ is closed to new replies.