# Hide Past Events From Site Search

> Removes events that have already ended from your site's search results, while keeping posts, pages and upcoming events, when The Events Calendar is active.

- Type: PHP
- Runs: Run everywhere
- Category: [Plugin Integrations](https://wpide.com/library/category/integrations/)
- Tags: events-calendar, search, past-events
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/tec-exclude-past-events-search/

## Code

```php
add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_search() || ! class_exists( 'Tribe__Events__Main' ) ) {
        return;
    }

    $meta_query   = (array) $query->get( 'meta_query' );
    $meta_query[] = array(
        'relation' => 'OR',
        array( 'key' => '_EventEndDate', 'compare' => 'NOT EXISTS' ),
        array( 'key' => '_EventEndDate', 'value' => current_time( 'mysql' ), 'compare' => '>=', 'type' => 'DATETIME' ),
    );

    $query->set( 'meta_query', $meta_query );
} );
```

## Notes

Past events stay reachable through the calendar's own past-events view.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Hide Past Events From Site Search" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
