skip to Main Content

How to Display Past Events using the Events Calendar Plugin

Below is a sample function you could use to query past events from the Events Calendar Plugin (Tribe Events). This function would be used as a callback in one of the theme's elements such as the Post Cards element via the Advanced Query setting. This could be useful if you want to create a separate page to display all past events if you are currently excluding past events from the grids.

/**
 * Past Events Query Arguments.
 *
 * @link https://total.wpexplorer.com/docs/snippets/query-past-tribe-events/
 */
function myprefix_past_events_query_args() {

	$query_args = [
		'post_type' => 'tribe_events',
		'posts_per_page' => '12',
	];

	if ( ! function_exists( 'tribe_get_events' ) ) ) {
		return $query_args;
	}

	$past_events = [];

	$get_past_events = tribe_get_events( [
		'end_date' => 'now',
	], false );

	if ( $get_past_events ) {
		foreach ( $get_past_events as $past_event ) {
			$past_events[] = $past_event->ID;
		}
	}

	if ( ! isset( $query_args['post__not_in'] ) ) {
		$query_args['post__in'] = $past_events;
	}

	return $query_args;
}
All PHP snippets should be added via child theme's functions.php file or via the Code Snippets Plugin (or alternative plugin)
Back To Top