Support

Home Forums Community Forum Show expired events only

Show expired events only

Posted: October 30, 2015 at 4:56 am


Olaf Hassenrueck

October 30, 2015 at 4:56 am

Hi there,
since I see this question a lot in the forums here and I had the same issue, I thought I might share my solution for this with you.

I am working with the wp_schedule_event function to check once a day, which events are expired and add them to a certain category.

Please note: you will have to create the expired category first and edit the code with the slug. Also you should keep in mind, that this queries ALL events except those which are already expired and that could cause a massive load time once the event is fired. (The wp_schedule_event function is not like a real cron job, so it fires on pageload if the timelimit set is reached).

Code above: No guarantee it is working.

// CRON JOBS
add_action( 'wp', 'expired_activation' );
add_action( 'update_expired_events_hourly', 'update_expired_events' );

function expired_activation(){
	if ( !wp_next_scheduled( 'update_expired_events_hourly' ) ) {
		wp_schedule_event(time(), 'hourly', 'update_expired_events_hourly');
	}
}

function update_expired_events(){
	// events query
	$args = array(
		'post_type'     	=> 'espresso_events',
        'post_status'   	=> array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
        'post_per_page'		=> -1,
        'tax_query'	=> array(
        array(
            'taxonomy'  => 'espresso_event_categories',
            'field'     => 'slug',	
            'terms'     => 'ee_exp', // the expired category slug
            'operator'  => 'NOT IN',
            ),
       )
	);
	$events = new WP_QUERY($args);
	$today = strtotime(current_time( "Y-m-d H:i:s", $gmt =0 )); // get current date
	 
	 if ( $events->have_posts() ){
	 	 while ( $events->have_posts() ){
	 	 	$events->the_post();

		 	//get event end date
			$event_end = strtotime( espresso_event_end_date( 'Y-m-d', 'H:i:s', false, false ) ); // get event end date
			
			if( $event_end - (60*60*24) < $today ){
				wp_set_object_terms( get_the_ID(), 'ee_exp', 'espresso_event_categories', true); // Add post to expired category
			}
		 
		 }
	 }
}


Olaf Hassenrueck

October 30, 2015 at 4:57 am

Oh and please note, that the example code fires hourly, not daily, you’ll have to adapt it.


Olaf Hassenrueck

October 30, 2015 at 7:13 am

Ignore this, found a bug, I’ll let you know when I have it fixed


Josh

  • Support Staff

November 3, 2015 at 2:12 pm

Hi Olaf,

Here’s another suggestion you can use in a WordPress page template:

https://gist.github.com/joshfeck/61a9e21bbc9d4111fbdf

The support post ‘Show expired events only’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso