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
- Should I use Pre Get Posts or WP_Query
- Using pre_get_posts with WP_Query
- Post_count only shows the number of results per page
- WP Query where title begins with a specific letter
- What is “main query”? [duplicate]
- 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
- Changing Posts Per Page and offset with pre_get_posts
- Multiple orderby parameters in pre_get_posts() action
- Pagination not working for Custom search form & custom result template
- Pagination broken when merging search results with additional WP_Query
- 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
- Multiple keyword search
- Single page theme that uses pages for the content
- Using next/previous_posts_link with customised search
- Add custom search results to main WP search [duplicate]
- WP_Tax_Query with post_tag not working
- Using pre_get_posts on a specific core/query block
- 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?
- Modifying the Search Results
- 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
- Offset Page Loops and Pagination
- pre_get_posts order by not working
- Adding a parameter to the default query
- Using WP_Query in “parse_query” or “pre_get_post” in Posts2Posts
- Custom Search Result Page displaying only 1 result.
- Querying on multiple taxonomies pulled from $_GET checkbox array not working?
- Is there any advantage of using default WordPress Search instead of creating a custom one?
- Search pages that are a child of the current page
- Include tags in WP_Query
- Display posts from only one post form in custom query and exclude in main query
- Complex WP SQL Query
- Custom Search Not Consistent with Results
- Editing the default wordpress search
- How to use wp_query to post count by search term > group post count by date > convert to json. (ex. result 12-21-2020 – 343)
- Extend product search with meta in WooCommerce
- Search for “2nd” also returns “22nd” in wp_query
- Add filter post_where and passing post_type argument
- Woocommerce – exclude only older out-of-stock items
- WP Query – order posts by meta field first and then order the rest
- wp_query search not taking keywords with together for multiple words
- Wildcard search in WP Query
- Search Page Returns Nothing
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- Redirect Search to Form When No Product Results Are Found
- WP_Query – How to get all posts of specific days of week by custom field date?
- wp_query show all values
- change posts_orderby of the callback wp_query?
- Search.php – return number of results but cannot loop through
- tax_query in pre_get_posts
- Exclude post with taxonomy and from the terms (taxonomy & tag) posts count
- WordPress search WP_Query to cover multiple post types and their custom fields?
- Replace wp_query with wp_user_query
- How to prevent WP_Query from filtering on ‘s’ but keep ‘s’ for other purposes?
- Make a products only viewable to a specific user ID in meta_query pre_get_posts
- Add dynamic search to paginated WP_Query
- Pagination repeating posts on search results
- Categories In English version showing not canonical URL, instead shows query search result
- order posts by a secondary query that counts items
- Filter admin ajax data by url query
- Display the search results like the original pages
- How to use jQuery validation to set the search bar to accept only a specific input?
- Search with WP_Query, but ignore href URLs in anchor tags?
- Woocomerce – Order products by float attribute in archive pages
- Order posts by title and custom field value?
- Order & Orderby clause not working with custom query
- Using the same WP_Query for shop and widgets in WooCommerce shop
- How to make the default WordPress search return borader results
- WordPress WP_Query offset parameter not working with search parameter
- Adding a Theme Customizer Control for Posts_Per_Page
- Manipulating a query
- Looking for a way to exclude frontpage and nav menu from query filter
- pre_get_posts redirecting
- Custom search: Search posts but display parent page in results
- Query posts by custom fields
- Searching in specific custom post type
- Search (Custom Form, Custom Search Result)
- Help with showing text when something is entered in my search bar
- WordPress WP_Query Search (‘s’) With Multiple Search Terms
- Search results stuck on page 1
- Custom Taxonomy in custom REST API search
- How to put posts with some taxonomy on top of others in `pre_get_posts`