Automating deleting old events – works sometimes
-
I have a wp-cron job that runs each night, to delete past events.
It does not delete recurring events.
It calls EM_Events::get() and then goes through the returned list can calls $event->delete().Unfortunately the deleting part frequently stops working i.e. events after a certain date will not be deleted. It seems like there must be a problem on a certain event and then trying to delete events later in the list does not work.
I tried collecting all the IDs of the events to be deleted in an array and then calling:
EM_Events::delete($array_of_past_event_ids)
but that did not delete anything.I am comfortable digging into the EM code so I am happy to try to any suggestions people may have.
Extract of the code is below:
<?php // See: https://wp-events-plugin.com/documentation/event-search-attributes/ $past_events = EM_Events::get(array('scope'=>'past','orderby'=>'event_start_date')); foreach ($past_events as $event) { $recurring = $event->recurrence_id? 'Yes' : ''; $location = new EM_Location($event->location_id); $event_info = sprintf('<li>%s at %s on %s</li>%s', $event->event_name, $location->location_name, $event->event_start_date, "\n"); // Add to appropriate list depending on whether the event is a recurring one. if (!$event->recurrence_id) { $deleted_events_list .= $event_info; $events_to_delete_array[] = $event->post_id; // Add this event to the list to be deleted. $count++; $event->delete(); // Delete single event (because EM_Events::delete($events_to_delete_array) is not working. // or //EM_Events::delete(array($event->post_id)); } unset($location); } ?>
- The topic ‘Automating deleting old events – works sometimes’ is closed to new replies.