How Do I Redirect Past Events?

How do I automatically redirect events, once their date has passed, to another page?

  • Kasia Swiderska
    • Support nomad

    Hello Mary,

    Please try this code:

    <?php
    function wpmu_redirect_events() {
        global $post;
        if ( !empty( $post->post_type ) && 'incsub_event' === $post->post_type && is_single() ) {
            $event = new Eab_EventModel($post);
            $end = $event->get_end_timestamp();
            if ( $end && $end < time() ) {
                wp_redirect( home_url( ) );
                exit();
            }
        }
    }
    
    add_action( 'wp', 'wpmu_redirect_events' );

    Replace home_url( ) with page url you want to do redirect

    wp_redirect( 'https://yoursite.com/page' );

    use this code as MU plugin https://codex.wordpress.org/Must_Use_Plugins

    kind regards,
    Kasia

  • Mary
    • Wisp of the WP Loop

    Thank you. It doesn't work. It shows a blank space where page should be with "No event found" and "legend".
    I created mu-plugin called events-redirect-expired-events.php

    I tried changing the URL to the main Events page https://christiangays.com/events-list/

    When that didn't work I created a new page at https://christiangays.com/past-events but it doesn't work either. Both give me the same (no) page. See screenshot.

    Support Access is granted.

  • Kasia Swiderska
    • Support nomad

    Hello Mary,

    Can you provide more information on how this redirect should work? From your initial post it seemed to me that you want to redirect when someone goes to single, past event - but now I'm not sure now if this is what you are looking for.
    Also please paste here code on how you used it on your site with change of url and I will test it again.

    kind regards,
    Kasia

  • Mary
    • Wisp of the WP Loop

    The issue is crawl errors from Google Search Console for events that have passed such as:
    https://christiangays.com/events-listing/2018/11
    https://christiangays.com/events-listing/2018/8
    https://christiangays.com/events-listing/1324/06/
    https://christiangays.com/events-listing/2017/5
    The target URL doesn't exist, but your server is not returning a 404 (file not found) error. Learn more
    Your server returns a code other than 404 or 410 for a non-existent page (or redirecting users to another page, such as the homepage, instead of returning a 404). This creates a poor experience for searchers and search engines.

    I am posting events on Events+, but once the event date passes, the event gets removed and Google Search Console gives the above error.

    I need this situation to be fixed please.

  • Kasia Swiderska
    • Support nomad

    Hello Mary,

    Those links

    https://christiangays.com/events-listing/2018/11
    https://christiangays.com/events-listing/2018/8
    https://christiangays.com/events-listing/1324/06/
    https://christiangays.com/events-listing/2017/5

    are not links to separate events (and code was for that - but our assumption was that those events are existing and there is redirect needed when accessing them). Those are archives for dates. Those are virtual pages and they do exist, but because you removed events that passed they don't show anything.
    Also those:

    https://christiangays.com/events-listing/2018/11
    https://christiangays.com/events-listing/2018/8

    are not past, they are future - those are for 2018 year events. It suggests that you have recurring events set for year 2018 (so there was instance for them and was indexed), but somehow they were be removed with past event.
    Do you have custom code for removing past events? Do you have any php modifications in Events+ plugin? Let me know.

    kind regards,
    Kasia

  • Mary
    • Wisp of the WP Loop

    <<Do you have custom code for removing past events?>>
    Yes. I was given the above code by you on Oct 1. I created an mu-plugin called events-redirect-expired-events.php and I changed wp_redirect( home_url( ) ); to wp_redirect( 'https://christiangays.com/past-events/' );

    I also have mu-plugin for "edit-event-move-venue-under-title.php" and "events-remove-takes-place-on.php"

    In my functions.php I have "Events+ Fix Formatting of Dates" and "Add "Include All Details of Event Below" in /edit-event/"

    There is also a huge number of formatting fixes in Appearance -> Custom CSS

    <<Do you have any php modifications in Events+ plugin? >>
    Yes, many!

  • Kasia Swiderska
    • Support nomad

    Hello Mary,

    I'm terribly sorry for late response on my side - your post slipped through the cracs - I will make sure that won't happen again.

    Yes. I was given the above code by you on Oct 1. I created an mu-plugin called events-redirect-expired-events.php and I changed wp_redirect( home_url( ) ); to wp_redirect( 'https://christiangays.com/past-events/' );

    Code provide by me does not remove past events. It redirects them to page you created and put in code.
    This is link to past event https://christiangays.com/events-listing/2017/05/silver-lake-oh-shawn-thomas-concert/ - if you open it without code, you will see event. If you open it with your code, you will be redirected to page https://christiangays.com/past-events/

    I tested that code on your site and it does redirect correctly when user tries to open past event.

    https://christiangays.com/events-listing/2018/11
    https://christiangays.com/events-listing/2018/8
    https://christiangays.com/events-listing/1324/06/
    https://christiangays.com/events-listing/2017/5

    You have "Immediately expire past events" add-on enabled. This add-on removes events from archive lists. Like for example this one https://christiangays.com/events-listing/2017/5/ - if you still want to show events on those lists you need to disable this add-on and only use code for redirecting past events.

    So first two links are not past archives but future one. And in those months you don't have any events - but those dates are existing virtual pages.

    In both cases of archive there are no events there so what Google Console reports is Soft 404 error (you can read here more about that https://www.rebelytics.com/crawl-errors-google-search-console/#what-are-soft-404-errors )
    Pages exist - but Google see their content as "thin" and thinks they should be returning 404 error.

    Solution would be to still keep events on archive pages and use code to redirect single events and for future events write custom solution that would redirect archive pages (where there is no events) to another page (but we don't have this type of code and I need to check with developers if this is possible to do with quick code snippet).

    That can also be done manually in htaccess by doing redirects for archive pages:

    Redirect 301 /events-listing/1324/06/ https://christiangays.com/past-events/

    I will consult developer about the redirect for future archive pages and let you know about their response.

    kind regards,
    Kasia

  • Mary
    • Wisp of the WP Loop

    This is not a satisfactory solution. I cannot add the hundreds of past events manually to my htaccess file, and I do not want to have to continue to add them manually every day. This is not a reasonable solution!

    Can you please refer this ticket to Adam or James Morris or Predrag!!!!

    I will reiterate.

    I currently have Events+ settings for events to be removed from the current listings page after their stop date. I don't care if they are deleted or if they get archived. I just don't want them on the current events page, which only makes sense.

    The problem is that when they expire they give 404's. This needs to stop! Past events must be dealt with automatically. They can be deleted, in which case they should be redirected to https://christiangays.com/past-events/ or they can be archived according to date.

    I would appreciate it if Adam or James Morris or Predrag would give me a solution.
    Thank you.

  • Predrag Dubajic
    • Support

    Hi Mary,

    I had a chat with one of our devs about this and below code should do the trick:

    <?php
    function wpmu_events_redirect() {
        global $wp_query;
        if ( !empty( $wp_query->query_vars['year'] ) && !empty( $wp_query->query_vars['monthnum'] ) && empty( $wp_query->query_vars['day'] ) ) {
            if ( strtotime( "{$wp_query->query_vars['year']}-{$wp_query->query_vars['monthnum']}-01" ) <
                    strtotime( date( "Y-m-01" ) )
            ) {
                //redirect if month is in the past
                wp_redirect( home_url() );
                exit();
            }
            if     ( ( strtotime( "{$wp_query->query_vars['year']}-{$wp_query->query_vars['monthnum']}-01" ) >=
                    strtotime( date( "Y-m-01" ) ) && empty( $wp_query->posts ) )
            ) {
                //redirect if month is in the future or current month but it's without event
                wp_redirect( home_url() );
                exit();
            }
        }
    }
    add_action( 'wp', 'wpmu_events_redirect' );

    As always, you can add this in your child theme functions.php or as a mu-plugin.

    Let us know how it goes :slight_smile:

    Best regards,
    Predrag

  • Predrag Dubajic
    • Support

    Hi Mary,

    I was looking a bit more into this and actually think that maybe that first solution that Kasia provided will be a best way to do it.

    Currently it's not working because you have "Immediately expire past events" add-on enable which expires your past events and removes them from archives.

    So what you can do is go to Events > Settings > Add-ons and disable this addon.
    Create mu-plugin again with the code that Kasia provided in her first response.
    Now the events will be in archive so google shouldn't report any empty pages anymore but when you try to access past events they will redirect to the page that you have set in your mu-plugin.

    Can you try it out and see if that works for you?

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.