Redirect Expired Events

This topic contains 5 replies, has 2 voices, and was last updated by  Kiran 5 years, 12 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #430811

    Dale
    Full Member
    Post count: 96

    Good day,

    I am hoping you can assist in improving my 404 pages generated from expired events.

    I am trying to create a function that will redirect expired events in a particular category (that will usually display a 404 page) to the root category. i am using multi-city and my redirect action is not working.

    I have the following code:

    
    
    // Expired Events Redirect
    add_action( 'template_redirect', 'expired_events_redirect' );
    function expired_events_redirect()
    {
        // check if is a 404 error, and custom post type events and category
        if( is_404() && is_singular('gd_event') && is_category( 'festival' ) )
    //    if( is_404() && is_singular('your-job-custom-post-type') )
        {
            // then redirect to
            wp_redirect( home_url( '/events/festival/' ) );
            exit();
        }
    }
    

    Do you think this is possible. Am I missing something.

    You assistance is much appreciated.

    Kind regards,

    #430844

    Kiran
    Moderator
    Post count: 7069

    Hi,

    Try this one:

    
    
    function expired_events_redirect_setup( $post, $query ) {
        global $wp, $gd_expired, $wp_query;
        
        if ( !empty( $post ) && !empty( $query->is_single ) && $post[0]->post_type == 'gd_event' ) {        
    
    		$check_is_expired = false; // DO STUFF HERE TO CHECK HERE EXPIRED OR NOT
            
            if ( $check_is_expired )  {
                $gd_expired = $post[0]->ID;
                $wp_query->is_single = true;
            }
        }
        
        return $post;
    }
    add_filter( 'posts_results', 'expired_events_redirect_setup', 10, 2 );
    
    function expired_events_redirect() {
        global $wp, $post, $wp_query, $gd_expired;
       
        if ( is_404() && $gd_expired ) {
            $gd_post = get_post( $gd_expired );
            
            if ( !( !empty( $gd_post ) && $gd_post->post_type == 'gd_event' ) ) {
                return;
            }
    
    		$default_category_id = geodir_get_post_meta( $gd_post->ID, 'default_category', true );
            $term = $default_category_id ? get_term( $default_category_id, $gd_post->post_type . 'category' ) : '';
            $default_category = !empty( $term ) && !is_wp_error( $term ) ? $term->slug : '';
    
    		if ( $default_category ) {
    			$redirect_url = get_term_link( $default_category );
    		} else {
    			$redirect_url = home_url();
    		}
    
    		wp_redirect( $redirect_url );
            exit();
        }
    }
    add_filter( 'template_redirect', 'expired_events_redirect' );

    Kiran

    #430949

    Dale
    Full Member
    Post count: 96

    Hi Kiran,

    Thanks for taking a shot at this.

    Your code does not seem to do anything on my site regarding 404’s for Expired/Draft events although I see where you are going with this but unfortunately my PHP skills are lacking in this regard.

    I am not sure how events are tagged ‘Expired’ in GD. My site is set to ‘Expired > Draft’ and not ‘Expired > Trash’ although both instances will obviously result in a 404. Not sure if that makes any difference.

    Any further input appreciated.

    #430972

    Kiran
    Moderator
    Post count: 7069

    I just given code with basic logic that you can follow. you have to write code to check event expire or not at “// DO STUFF HERE TO CHECK HERE EXPIRED OR NOT”.

    Event can be expired via package or via event date. which one you following?

    Kiran

    #431037

    Dale
    Full Member
    Post count: 96

    Hi Kiran,

    My events expire via package ‘alive days’.

    I was not aware that events could expire when the date of the event passes. Is this a new feature?

    Would love to sort out my analytics 404’s though.

    Many thanks

    #431079

    Kiran
    Moderator
    Post count: 7069

    Hi Dale,

    Use following code to find listing is expired or not. Replace “$check_is_expired = false; // DO STUFF HERE TO CHECK HERE EXPIRED OR NOT” in previous code.

    
    
    $post_expire_date = geodir_get_post_meta( $post[0]->ID, 'expire_date', true );
    $check_is_expired = ( $post_expire_date != '0000-00-00' && $post_expire_date != '' && geodir_strtolower( $post_expire_date ) != 'never' && strtotime( $post_expire_date ) < strtotime( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ) );

    This is customization and it is beyond support, so we can help you more in code customization. You can hire developer from http://geodirectoryexperts.com/

    Kiran

Viewing 6 posts - 1 through 6 (of 6 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket