Showing Past Events in Reverse Order

The calendar’s list and photo views show past events in chronological order by default. That means the oldest events are displayed first and get newer as you go. If you like to show the events in reverse order, where the newest events are displayed first, you can use this snippet.

Please try adding the following lines of code to the site. Add this to your theme’s functions.php file and see if this one helps reverse the order of past events.

This one is a bit tweaked as it takes the date parameter of the shortcode into account.

				
					// Showing Past Events in Reverse Order

add_filter(
    'tribe_events_views_v2_view_template_vars',
    function ($template_vars, $view) {
        $context = $view->get_context();

        // Not doing a shortcode - bail.
        if (empty($context->get('shortcode'))) {
            return $template_vars;
        }

        // Get the date set for the shortcode and today's date
        $targetDate = strtotime($template_vars['request_date']->format('Y-m-d'));
        $currentDate = strtotime(date("Y-m-d"));

        // If the date set in the past set is_past true
        if ($targetDate < $currentDate) {
            $template_vars['is_past'] = true;
        }

        // reverse order if is_past true
        if (!empty($template_vars['is_past'])) {
            $template_vars['events'] = array_reverse($template_vars['events']);
        }

        return $template_vars;
    },
    8,
    2
);
				
			

Related article