How can we help?
How to delete an expired event by time
In Order to display an event as Expired after a specific date and time, you need to add the following code to your theme functions.php file.
- Open your child theme,
- Add the code into the funtions.php file.
/* Event Expire by time */ function my_cron_schedules($schedules){ if(!isset($schedules["5min"])){ $schedules["5min"] = array( 'interval' => 560, 'display' => __('Once every 5 minutes')); } return $schedules; } add_filter('cron_schedules','my_cron_schedules'); function schedule_my_cron($args){ wp_schedule_event(time(), '5min', 'my_schedule_hook'); } if(!wp_next_scheduled('my_schedule_hook')){ add_action('init', 'schedule_my_cron'); } add_action('my_schedule_hook','my_schedule_hook'); function my_schedule_hook() { $args = array( 'post_type' => 'event_listing', 'post_status' => 'publish', ); $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => '_event_end_date', 'value' => current_time('Y-m-d H:i:s'), 'compare' => '<=', ), ); $events = new WP_Query( $args ); if ( $events->have_posts() ){ while ( $events->have_posts() ) : $events->the_post(); wp_update_post(array( 'ID' => get_the_ID(), 'post_status' => 'expired' )); endwhile; } }
- Next, create one field with the field type “Time” in the field editor at the admin panel.
- This would be visible on the event submission form, from where you can mention the time at which the event must expire.