Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.