Skip to content

The Events Manager

The Events Manager plugin for WordPress is a power piece of software that’s incredibly easy to manipulate and expand.

One of it’s annoying quirks is if you want to write your own custom loops, events that have passed show up.

We I wrote this little snippet for wp-cron to take care of that little annoyance. It saves me work, so gj Mark.

function schedule_expire_events()
{
if (!wp_next_scheduled('expire_past_events_hook'))
{
wp_schedule_event(time(), 'every_six_hours', 'expire_past_events_hook');
}
}
add_action('wp', 'schedule_expire_events');

function expire_past_events()
{
global $wpdb;
$current_date = current_time('mysql');
$events = $wpdb->get_results("SELECT post_id FROM {$wpdb->prefix}em_events WHERE event_end_date < '$current_date' AND post_status = 'publish'");
foreach ($events as $event)
{
wp_update_post([
'ID'=> $event->post_id,
'post_status'=> 'draft',
]);
}
}
add_action('expire_past_events_hook', 'expire_past_events');

function unschedule_expire_events()
{
$timestamp = wp_next_scheduled('expire_past_events_hook');
if ($timestamp)
{
wp_unschedule_event($timestamp, 'expire_past_events_hook');
}
}
register_deactivation_hook(__FILE__, 'unschedule_expire_events');

Mark Hall

Comments (0)

Leave a Reply

Back To Top