By default, The Events Calendar displays ongoing events even if they have already started, as long as they have not ended. This is useful for multi-day events, but in some cases, you might want to hide events as soon as they begin.
In this guide, you’ll learn how to prevent events from appearing on the main calendar views if their start date/time is in the past. We’ll show you how to use the pre_get_posts action to filter the main query and respect the site’s timezone settings.
Use Case
This is helpful if:
- You only want to show upcoming events.
- You don’t want events to remain visible after they’ve started.
- You need to make sure the comparison uses the correct timezone as set in WordPress.
How It Works
We’ll hook into WordPress’s pre_get_posts action and modify the main query that loads events. The logic ensures that:
- It only affects frontend views.
- It uses the site’s configured timezone.
- It filters out events that started before the current time.
PHP Snippet: Hide Already Started Events (Timezone-Aware)
Add the following code to your theme’s functions.php file or use the Code Snippets plugin:
add_action( 'pre_get_posts', 'hide_already_started_events_using_timezone' );
function hide_already_started_events_using_timezone( $query ) {
// Only target the main query on the frontend
if ( is_admin() ) {
return;
}
// Get the current time in the site's timezone
$timezone_string = get_option( 'timezone_string' );
$timezone = $timezone_string ? new DateTimeZone( $timezone_string ) : wp_timezone(); // fallback
$now = new DateTime( 'now', $timezone );
$today = $now->format( 'Y-m-d H:i:s' );
// Modify the query to exclude events that have already started
$meta_query = $query->get( 'meta_query' );
if ( ! is_array( $meta_query ) ) {
$meta_query = [];
}
$meta_query[] = [
'key' => '_EventStartDate',
'value' => $today,
'compare' => '>=',
'type' => 'DATETIME',
];
$query->set( 'meta_query', $meta_query );
}
Result
This will remove any events from calendar views if their start date/time is in the past. Events will no longer be listed once they begin, even if they are still ongoing.
Important Notes
- This affects all frontend event queries, including List, Month, and Day views.
- This does not affect single event pages. Events can still be viewed directly via their permalink.