0

For example, my wordpress website is www.example.com. If I type example.com/abcdef or any other url that does not exist, it redirects to my custom 404 page without any issues.

However, when I search something, the url changes to www.example.com/?s=abcdef and it shows wordpress's default 'Nothing Found' page. Is there a way I can redirect a wrong search's to my custom 404 page or can I change wordpress 'Nothing found' page?

Thanks

  • Do you want to disable example.com/?s=* completely having that query string hit your custom 404? – Liam Stewart Jan 13 '18 at 22:26
  • Thanks for replying. I don't want to disable 'example.com/?s=*' completely if it screws up the website's search function. I am looking to change the page that shows up when the search doesn't get any results. If I can customize it and add some of my custom text and image to it that would be better. If not, can it redirect to the error 404 page? Thanks – Nobble Jan 13 '18 at 22:31
2

A search shows the search template regardless of whether or not there are any results, which is either search.php or index.php if that template doesn't exist.

If you want to load an entirely different template, you can use the search_template filter. Assuming your theme's 404 template is 404.php:

function wpd_search_template( $template ) {
    if( ! have_posts() ) {
        $template = locate_template( array( '404.php' ) );
    }
    return $template;
}
add_filter( 'search_template', 'wpd_search_template' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.