最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to redirect a wrong search on my wordpress website to the error 404 page?

programmeradmin2浏览0评论

For example, my wordpress website is www.example. If I type example/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/?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

For example, my wordpress website is www.example. If I type example/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/?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

Share Improve this question edited Jan 13, 2018 at 22:32 Temani Afif 6835 silver badges13 bronze badges asked Jan 13, 2018 at 22:23 NobbleNobble 33 bronze badges 2
  • Do you want to disable example/?s=* completely having that query string hit your custom 404? – Liam Stewart Commented Jan 13, 2018 at 22:26
  • Thanks for replying. I don't want to disable 'example/?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 Commented Jan 13, 2018 at 22:31
Add a comment  | 

1 Answer 1

Reset to default 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)

  1. 暂无评论