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
- How to redirect non-logged in users to a specific page?
- When I change slug, will WordPress create redirect from the old URL?
- Why am I getting a 301 on a existing post?
- Keeps on redirecting on my development copy
- Password change when the user login first time
- Custom login form redirect to external site
- Exclude a path from WordPress using .htaccess redirects (Apache)
- Best way to handle redirects
- Redirect to custom URL after registering from a page with registration form
- Stop unwanted WP redirection to new url
- WordPress Customizer: custom redirection after closing
- htaccess 301 redirect http to https doesn’t work
- Site redirecting from http to https [closed]
- Use htaccess to redirect WordPress to static website in a subfolder
- How to redirect URL with subfolder to the same URL but without subfolder?
- Site loads very slowly (4-5 minute load time)
- If User is NOT Logged in and Page ID is not 6 – Redirect to Login
- Snippet for own (gr)avatars
- 503 Service Unavailable error was encountered
- SSL and www to non-www redirection works only on homepage – WordPress
- How to redirect if a background request is still being executed
- WordPress keeps redirecting subpages without http to https homepage after switch
- ReDirect subfolder link to another sub-folder and force SSL
- Two WordPress installations: One in root and one in sub directory – no images in subdir installation [closed]
- Redirect user to original url after login?
- A WordPress site with more than one domain name / Parked domains
- Redirect user if they are not logged in
- BuddyPress – Redirects on Login and Logout
- Redirect user on first visit based on geographical location
- Bedrock WordPress
- How to prevent redirect 301
- Redirect one domain to another
- Only execute function if SSL is set up properly
- How to redirect wp_login_form to new page after user loged in?
- How do I Redirect Tags from Old Joomla Site to New WordPress Site?
- How to redirect user after login to the page called by his first name?
- Redirect after get_delete_post_link
- Whenever I try to publish a post in wordpress, I always get redirected to the installation page…why?
- Login redirect to previous page
- Relink Drupal Permalinks “…node/XXXX” to wordpress Permalinks (e.g./audio/)
- Hide a Post everywhere except on redirect
- Redirect Image URL to it’s parent post
- Redirect user if they are logged in
- URL redirect regular expressions redirecting all after first viriable
- Redirect not work
- There is 1 redirect
- How to create mass 301 redirects with PHP in Nginx server without using a WP plugin
- Redirect HTTP request to HTTPS request
- Woocommerce login to specific page or referrer
- 301 Redirects when change URL in WordPress database
- Tag archive page not working
- How to move a https website back to dev and access the admin?
- Custom Form Redirects to Post after Submit
- Plugin redirects URL structure using 301 redirect. Time to disable the plugin?
- URL is redirecting (301) when query string is passed through URL
- Plugin html-on-pages: URL not redirecting
- Redirect a WP to another with all URL
- Regex for pdf’s – Old .asp Site folder structure to new WordPress Media Folder(s)
- Redirect if a user has not written a post
- Create a custom redirect link that tracks the first visited page and after submitting form goes to first page using formidable forms plugin? [closed]
- WordPress not use site url when redirect 301