This will do exactly what you’re asking for:
add_action('wp', 'redirect_empty_search');
function redirect_empty_search() {
global $wp_query;
if( isset( $_REQUEST['s'] ) && 0 === $wp_query->post_count ) {
wp_redirect( home_url() );
exit;
}
}
You’ll want to add some kind of message on the homepage when this happens, maybe by adding a query parameter to home_url() and displaying a message in your theme when that parameter is set. Something like:
wp_redirect( add_query_arg( 'searchfailed', '1', home_url() ) );
Related Posts:
- how to limit search to post titles?
- How does WordPress search work behind the scenes?
- Remove some pages from search
- WordPress blog with 30 000 posts: poor search performance
- How do I filter the search results order?
- Link to search page without search parameter
- Search/Replace for editor in HTML-mode
- Do I need to sanitize WordPress search query?
- Neither the_excerpt() nor the_content() works properly?
- Add custom parameters to JSON API search query?
- How to stop _wpnonce and _wp_http_referer from appearing in URL
- Return all Tags from search results
- Search results with custom loop don’t update when paged
- Set minimum number of characters in the search
- search.php template not loading results
- Does WP search natively return results sorted by type (post, post type, page, category…)?
- How do I properly redirect outbound links with special characters
- Conditional tag search-no-results
- Search Issue in the theme
- How do I make search results include ONLY pages, no posts?
- Display Categories in Search Results
- intellisense in wp search
- Is it possible to search authors by profile field?
- Search result conditonal tag
- Change loop.php for empty search customization
- Advanced Search by minimum/maximum values
- How to search in two different wordpress blogs by using a single searchform?
- Get search term not on searh results page?
- Custom Search Query running on every page
- Search Result from another wordpress website
- Remove WHERE conditions from search query
- Two Search Boxes: one all content, one only titles
- Change default search URL slug in wordpress with Custop Post Type as Search target
- Transliterating search term in another language
- Keep query string in url after executing a serch
- How to exclude events (custom posts) from search, if the start date has already passed?
- How to conditionally pass a parameter to wordpress search to limit post types?
- Customize search page
- Change WordPress search Permalink structure
- How to load WP categories into array for autocomplete?
- Custom SQL query on wp_users with the addition of wp_usermeta
- Use “search” in permalink?
- Media searching ignored
- Risk of sql injection for custom search form
- Generate list of tags from search results
- Use created page as search results page and custom template
- How do I search dynamically created content?
- Post 2 Post Site Search
- Using search.php without a ‘s’ field in searchform.php
- custom search results affected by _REQUEST set
- Search queries don’t seem to work?
- Array_search ignoring the first element [closed]
- Add Icon to Placeholder Text in Search Form
- Unexpected esc_html and esc_attr behaviour
- WordPress search page doesn’t work with either $_GET or $_POST
- Search Results No Link
- Search for two strings in WP search
- Sort search results by Custom Field
- Order search results by category
- Strip HTML Tags From Search Results
- How to build an advanced search form by post type?
- Should I create a parent page with no content? [closed]
- Disable character’s replace in search
- Why WP_Query’s ‘s’=>’keyword’ searches only for ‘post_title’ and why not ‘post_content’?
- adding more text to a query string
- Remove Page Timestamp via functions.php so it doesn’t show in Google Search Result Description [closed]
- Independent search tool that returns exact match of a post title
- Search using WP_Query
- Prevent searches less than 4 characters
- Changing search terms results a 403 – dsIDXpress Pro plugin [closed]
- Search wordpress using three different drop down menus
- How to get search result as image rather than page
- Search results – 404 error after hitting enter
- Redirect wordpress search query if its a tag
- Is it possible to show subdomain posts in main domain search result?
- Replacing word in wordpress Search not working Properly
- How to add search box to table results in admin page [closed]
- How might I go about using the API to search for a user by email, but ensuring that it is an exact match?
- Add CPT to search results with pre_get_posts
- Some search results link to main page instead of post
- Replacing wordpress search with custom code
- Use WordPress search in external static website
- Change output URL Search module
- search for single meta_key with numeric array of meta values
- How to customize default wordpress search query
- Customizing Google’s Search box in WordPress [closed]
- Insert DIV container below 1st search result
- I’ve messed up the admin search functionality. Help!
- Extend search function with link to searched word
- Content instead Excerpt on term
- Custom search results page not showing excerpt
- Error when searching without any results
- Listing parent section in search results
- Search results: How to add direct jump to anchor?
- Limit search to posts and pages
- How make Autocomplete with wp_query in post_type attachment
- WordPress Search – Reject partial matches
- Need help to be able to searching by tag and title
- Wp query search built out of 4 terms?
- Ivory Search error