Like others have said you can’t use pre get posts since there is no way of knowing if the search returned any posts. I would also say that the if else is a very clean way to do it and might be the better choice if anyone else is going to be working on this.
But if you really wanted to do it with a filter on either the posts_results or the_posts to return the replacement posts that should be shown if the search is empty.
<?php
add_filter('the_posts', 'np_replace_empty_search', 10, 2);
function np_replace_empty_search($posts, $wp_query){
if($wp_query->is_search && empty($posts)){
$new_query = new WP_Query();
return $new_query->posts;
}
return $posts;
}
Related Posts:
- Order Search Results Page by meta_value If no Value Return Remaining Results
- WordPress custom search form with pre_get_posts not work
- Searching through different categories on different pages code is not working
- Including only current user’s posts in search
- Sorting search results with custom dropdown
- $wp_query->found_posts not returning correct value
- Block internal search queries with pre_get_posts and regex rules
- Search Exact on WordPress – Relevannsi Solution Not Working
- Excluding posts from search results page with meta query not working
- Limit default Search query to post_title
- Search query alteration not working for meta values
- When to use WP_query(), query_posts() and pre_get_posts
- Should I use Pre Get Posts or WP_Query
- Can I force WP_Query to return no results?
- Using pre_get_posts with WP_Query
- Post_count only shows the number of results per page
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- WP Query where title begins with a specific letter
- What is “main query”? [duplicate]
- Reduce or prevent calling of update_meta_cache
- How to add taxonomy filter on the query fly?
- Search custom taxonomy term by name
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- How to uniquely identify queries?
- pre_get_posts with get_posts
- How-to exclude terms from the main query the most performant way?
- Changing Posts Per Page and offset with pre_get_posts
- Tracing the life of a query
- Want to redirect if search query match exact title of any post
- Slow SQL_CALC_FOUND_ROWS Query
- “pre_get_posts” firing on every query
- RSS feed with specific keyword
- Using is_main_query to select custom post type on certain page
- Multiple orderby parameters in pre_get_posts() action
- Pagination not working for Custom search form & custom result template
- How to support multiple search terms query within one process?
- SQL Statement generated by WP_Query not producing expected results
- Pagination broken when merging search results with additional WP_Query
- Custom search form with empty parameters
- How Do I Use WP_Query to Run This Database Query as Search Result?
- Sorting Posts by custom field
- How to pass custom parameter to WP_Query for filtering in pre_get_posts
- How to search by title or tags exclusively?
- WordPress search exact match
- Query with pre_get_posts to get pagination
- Pagination wont work with search results template [duplicate]
- Recommended way to drop a pending query (in pre_get_posts)?
- How do I search WordPress by different fields without a plugin?
- Multiple keyword search
- Single page theme that uses pages for the content
- Using next/previous_posts_link with customised search
- Search – Ajax – Alter Query Parameters with Pagination
- If orderby parameter using pre_get_posts is the same for multiple posts what fallback does the query use?
- Can not switch the queried post in pre_get_posts hook
- How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?
- Add custom search results to main WP search [duplicate]
- order by multiple meta keys in pre_get_posts
- Multiple search queries on one page
- How to sort posts in admin by titles with dd.mm.yyyy format?
- tax_query shows no results if nothing is selected
- WP_Tax_Query with post_tag not working
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to Modify Taxonomy Archive Page with Search Parameter?
- Using pre_get_posts on a specific core/query block
- How to display future posts – modified query still yields 404
- WP Query search for attachments and their exact title
- WP_Query’s “request” SQL Query
- pre_get_posts filter meta_query without conflicting existing meta_query
- When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?
- How to change posts order on Category and Tag archives?
- is_search was called incorrectly
- How do I search inside specific taxonomies in WordPress
- Modifying the Search Results
- Custom category search box for WordPress
- Refine search results using WP_Query
- How to find exact match for search term in WP_Query? What is the additional string added in LIKE query in WP_Query?
- Formulate a url to show posts with both taxonomy terms
- My Main Query Modification is Messing up my dynamic main – why?
- Search Posts with Custom Fields as query
- Offset Page Loops and Pagination
- pre_get_posts Remove tax_query Completely
- Complex date range with WP_Query and BETWEEN
- pre_get_posts order by not working
- Creating a custom search for a specific post type
- Adding a parameter to the default query
- Using WP_Query in “parse_query” or “pre_get_post” in Posts2Posts
- Single meta_query query using OR instead of AND in request’s WHERE statement
- Custom Search Result Page displaying only 1 result.
- How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?
- How to choose between hooking into per_get_posts or into parse_query
- How to loop for every result found in the_content() when using the search query?
- Assigning a meta_query value based on regexp to wp_query fails
- Store sticky post’s ids in a transient
- showing all search result in one template
- Querying on multiple taxonomies pulled from $_GET checkbox array not working?
- How to change main query based on post meta
- Is there any advantage of using default WordPress Search instead of creating a custom one?
- WP_Query Not Recognizing Taxonomy Parameter in Custom Search
- Search pages that are a child of the current page
- new WP_Query to get max price meta value not working