Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Return 404 in no-result page

  • Hello guys,

    I am getting lots of gibberish search result pages (ie. /search/5tryfhfskks ) indexed in Google.

    And I want to return 404 in my no-result pages (so I can safely remove these gibberish from Google index).

    What’s the easiest way to do this?

    I am already using a child themes now. But is there a function or filter we can apply to simply add the 404 header to the no-result.php?

  • Hi there,

    the template_redirect hook can be used to return the 404 template and a 404 status_header on no results.
    heres an example PHP snippet to do that.

    
    function custom_search_404() {
        global $wp_query;
        if ( $wp_query->is_search && $wp_query->is_main_query() && $wp_query->found_posts == 0 ) {
            $wp_query->set_404();
            status_header(404);
        }
    }
    add_action('template_redirect', 'custom_search_404');
    
  • Glad to hear that

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.