Skip to content

Snippet: 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://totalwptheme.com/docs/snippets/query-past-tribe-events/
 */
function myprefix_past_events_query_args() {
    if ( ! function_exists( 'tribe_get_events' ) ) {
        return [ 'post__in' => [ 0 ] ];
    }

    $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;
        }
    }

    $args = [
        'post_type' => [ 'tribe_events' ],
        'posts_per_page' => '50',
        'post__in' => $past_events ?: [ 0 ],
        'eventDisplay' => 'custom',
        'order' => 'ASC',
        'order_by' => 'event_date',
        'tribe_suppress_query_filters' => true,
    ];

    return $args;
}
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top