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' );
Related Posts:
- How to force a 404 on WordPress
- Redirect Restricted Page to 404
- How to prevent automatic redirection of 404 errors and “incorrect” URLs?
- How do I skip wordpress’s 404 handling and redirect all 404 errors for static files to 404.html?
- How do I programmatically generate a 404?
- How do I skip wordpress’s 404 handling?
- 404 redirection and Google’s index [closed]
- Redirect feed to 404 page
- Check if post is in draft or pending review?
- Why specific file directory fails to redirect but its parent folder succeeds?
- WordPress blocking/preventing viewing of .cgi file in subdirectory [closed]
- Deny access to a path (give 403 or 404 response)
- Redirect a page id url but not the page slug
- Defining a custom 404 for non-WordPress parts of site
- WordPress Auto-Redirecting on 404
- Built-In Postname Redirection vs Hard Redirects in relation to SEO and performance
- WP page 404 error only on mobile
- Force 404 redirect to home?
- How can I redirect users on the new 404 page without plugin?
- How to redirect an old page url to the homepage
- Dynamic 404 page content while still keeping 404 status code?
- Redirect to home if page doesn’t exists [duplicate]
- Redirect to parent page if child does not exist
- How to redirect non existent pages/posts in wordpress using htaccess
- How to Redirect WordPress domain.com/?anyword to 404 Page?
- How do I redirect the browser to 404 page, if no posts are found in home page
- How do i make WordPress return 404 instead of redirecting on misspelled page?
- How to redirect if 404 error in wordpress then redirect 404.php url
- Redirect to /404/
- Page editing in backend redirecting to 404 page not found with url /post.php
- How do I skip wordpress’s 404 handling?
- Subpages return 404 server error
- WordPress subfolders htaccess bypassing the root htaccess RewriteRule
- Why my htacess rewrite return 404?
- How to Redirect Url to homepage?
- Stopping media file redirects
- Forcing header cache control in 404 pages
- redirect to 404 page
- Redirect certain 404 pages to parent page
- Redirect of specific words in URL ahead of website migration
- Non-existent URLs/slugs redirect to default Posts page
- PHP header(Location: …): Force URL change in address bar
- What does HTTP/1.1 302 mean exactly?
- php refresh current page?
- Disable front end to use as CMS only?
- How to redirect non-logged in users to a specific page?
- Disable WordPress URL auto complete
- Host the wp-admin on another domain?
- Redirect user to original url after login?
- When I change slug, will WordPress create redirect from the old URL?
- WordPress localhost site redirect to live site
- WordPress redirecting connections on port 8080 to 80
- WordPress Redirect All HTTP requests to HTTPS via .htaccess
- wp_redirect() function is not working
- Redirect entire website to a single page
- My wordpress multisite homepage redirects to signup page
- Logout redirect to home page
- Disable ONLY URL auto complete, not the whole canonical URL system
- Redirect Localhost wordpress site to dashboard
- Redirect page URL to home URL without using a plugin
- Why is WordPress redirecting from http to https on a local environment?
- Login redirect to previous page
- How to prevent redirect to ‘About’ after core upgrade?
- 301 redirect instead of 404 when URL is a prefix of a post or page name?
- Why is wp_redirect() preferable to a standard PHP header redirect?
- Redirect to referring page after logging in
- User redirect to destination URL after login
- Prevent access to single post types
- check first time login only
- How to change the default logout link on WordPress Admin
- Stop WordPress from “guessing” redirects for nonexistent URLs
- WordPress install enters an infinite loop
- How to redirect a sucessful registration to a page template?
- How to configure nginx to redirect requests to the uploads directory to the production server?
- WordPress HTTPS redirect loop
- Why am I getting a 301 on a existing post?
- Redirect to dashboard after login
- Make a video post redirect to next post after completion
- Old blog/ and blog/feed/ URLs not working after moving blog to top-level via .htaccess
- Non-WordPress page in subdirectory under WordPress page
- Force HTTPS using .htaccess – stuck in redirect loop
- How to redirect after login, the working way?
- Keeps on redirecting on my development copy
- Password change when the user login first time
- Custom login form redirect to external site
- Redirecting WordPress /.htaccess / HSTS / SSL
- 301 redirect not working through .htaccess
- handling login/logout redirects
- Clarification on auth_redirect()
- Site redirects to wrong url when saving settings
- How to redirect all requests to wp-login.php?
- Implementing global redirection (automatic redirect for all pages who had their slug changed)
- Correct way to hide pseudo pages from being shown?
- Redirect blog page to latest post?
- Redirect to custom url when registration fails?
- Why does WordPress automatically redirect URLs with the parameter “name=” to a different page?
- Multisite 404 errors and non-www redirecting to home page
- prevent/block direct access to a thank you page
- Redirect After Delete User in Backend
- Turning Broken URLs Into Search Terms?