fbpx

How can we help?

How to delete an expired event by time

In Order to Expire the events by Time and not only date, add the following code in 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_expiry_date',
                'value'   => date( 'Y-m-d'),
                'compare' => '<=',
            ),
            array(
                'key'     => '_expire_time',
                'value'   => date( 'H:i A'),
                '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 field type “Time” in field editor at the admin side.
  4. This would be visible on the event submission form, from where you can now mention the time at which the event must expire.
Was this article helpful?
How can we improve this article?
Quick Links