fbpx
Did you miss our exclusive webinar on Attendee information? Click here to view the recorded session.
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.

  1. Open your child theme,
  2. 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;
    	        
    	}
    }
    
  3. Next, create one field with the field type “Time” in the field editor at the admin panel.
  4. This would be visible on the event submission form, from where you can mention the time at which the event must expire.
How To Delete An Expired Event By Time
Editorial Team

Our team constantly explores ways that technology can help us reinvent industries. We want to change the world by creating great products that transform industries. We Dream It, We Make It.

Quick Links
Close
Close