Page get parameter doesn't work with is_single() function

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();
        }
}

Topic template-redirect rewrite-rules functions url-rewriting redirect Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.