-
jerrylow
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');
-
David
Glad to hear that
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.