0

If exist the page parameter in the blog post, I have to redirect to the 404 page instead of current post URL. I tried to check the current page type (is singular) and try to get page parameter in the function.php, but I couldn't. It's doesn't work together. I need to implement it only for blog post. If possible .htaccess redirect also fine

I tried with this code but it doesn't work. But it's working without the is_single function. When I used is_single functions, the page parameter not returning.

Example Url: www.example.com/this-is-my-first-post?page=2 www.example.com/p=22?page=2

Thank you for your help. It's not working

add_action('send_headers', 'post_page_param_404_redirect');
function post_page_param_404_redirect() {
        global $wp_query;
        if (is_single() && isset($_GET['page'])) {
                $wp_query->set_404();
                status_header(404);
                get_template_part(404);
                exit();
        }
}

When I don't use the is_single function , It's working for all page, but I need it only for the blog post

add_action('send_headers', 'post_page_param_404_redirect');
function post_page_param_404_redirect() {
        global $wp_query;
        if (isset($_GET['page'])) {
                $wp_query->set_404();
                status_header(404);
                get_template_part(404);
                exit();
        }
}
1
  • send_headers is executed too early. pre_get_posts is the first hook that conditional tags can be used. Related question
    – nmr
    Commented Jun 27, 2021 at 22:12

1 Answer 1

1

The truth is when send_headers action is processing, the $wp_query isn't ready yet and you can't use is_single() function. But you can use it a bit later. There is a parse_query action, that can help you. It starts when the $wp_query is ready and comes out right after the send_headers action.

1
  • It's worked, thank you so much
    – MT09
    Commented Jun 28, 2021 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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