404 error – How to Redirect WordPress domain.com/?anyword to 404 Page?

Question

It is, in most cases, a logical thing to show the homepage even when there are querystrings available. Never the less, I’m assuming you have a reason for this. You could try code below to redirect your visitors to your 404 page:

if ( isset($_GET) ) {
    global $wp_query;
    $wp_query->set_404();
    status_header( 404 );
    get_template_part( 404 );
    exit();
}

Keep in mind that some other functionality might break because of this. You may want to add some additional conditionals.

0
STEP IADM 4 months 2022-04-07T07:36:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse