1

I am trying to redirect a page if there is a missing url parameter. I have found the following code from here:

function redirect_to_pending($query) {

    if($query->is_main_query() && $query->is_page(26)) {

        if(!array_key_exists('shazzid', $query->query_vars) || empty(get_query_var('shazzid'))) {

            wp_redirect(get_permalink(1752));
            exit;
        }  
    }
}
add_action('parse_query', 'redirect_to_pending');

It seems to work perfectly on any page except the homepage, where it generates the following error Notice: Trying to get property 'ID' of non-object in /wp-includes/class-wp-query.php on line 3956

0

However, to solve the issue, it seems the answer is to check the queried object is not NULL $query->get_queried_object() !== NULL

I had to remove the query_var because it turned the homepage into a 404. A separate issue.

// redirect if there's no job id
function redirect_to_pending($query) {

    if($query->get_queried_object() !== NULL && $query->is_page(26)) {

        if(!isset($_GET['shazzid'])) {

            wp_redirect(get_permalink(1752));
            exit;
        }  
    }
}
add_action('parse_query', 'redirect_to_pending');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.