php – WordPress template_redirect redirected too many times-ThrowExceptions

Exception or error:

I am writing a hook that will check for parameters in URL and make redirection.

The script redirects fine regarding the url, but the error appears in the browser.

www.sitename.sr redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

add_action('template_redirect', 'change_explore_url_for_cars');

function change_explore_url_for_cars(){

$type=$_GET['type'];

if($type == 'old_cars'){

    if(is_page('cars')&& isset($_GET['type']) && isset($_GET['sort']) ){ 
            wp_redirect(home_url('/cars/')."?type=old_cars&date_to=".date("Y-m-d")."&sort=newest");
            exit;
        }
    }

}

I dont know why is redirecting to manny times.

**UPDATE TO QUESTION, AND IT SOLVES THE PROBLEM OF REDIRECTION.

if($type == 'old_cars'){

    if(is_page('cars')&& isset($_GET['type']) && isset($_GET['sort']) && !isset($_GET['date_to'])){ 
            wp_redirect(home_url('/cars/')."?type=old_cars&date_to=".date("Y-m-d")."&sort=newest");
            exit;
        }
    }

}

After adding !isset($_GET[‘date_to’])) it solves the problem of redirection, as the statement if(is_page(‘cars’)&& isset($_GET[‘type’]) && isset($_GET[‘sort’])) is always true.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *