Redirect Long-Past Events to the Calendar
Sends visitors (and search engines) from single events that ended more than a set number of days ago to the main events page with a 301 redirect, keeping your calendar fresh.
Opens WPIDE on
Code
$wpide_days = 90; // Redirect events that ended more than this many days ago.
add_action( 'template_redirect', function () use ( $wpide_days ) {
if ( ! is_singular( 'tribe_events' ) || current_user_can( 'edit_posts' ) || ! function_exists( 'tribe_get_events_link' ) ) {
return;
}
$end = strtotime( (string) get_post_meta( get_queried_object_id(), '_EventEndDate', true ) );
if ( $end && $end < current_time( 'timestamp' ) - $wpide_days * DAY_IN_SECONDS ) { // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- event dates are stored in site time.
wp_safe_redirect( tribe_get_events_link(), 301 );
exit;
}
} );
Notes
Change $wpide_days. Logged-in editors can still view old events.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.