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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x