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

2
  • Do you want to disable example.com/?s=* completely having that query string hit your custom 404? Jan 13, 2018 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, 2018 at 22:31

1 Answer 1

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' );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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