Problem:
Custom searches initially display all results. How to change this so that it works like the Google homepage with no initial results until a search is made?
Solution:
There are two ways to do this. The simplest is to split the search form and search results onto separate pages (the search results page will still include the filters so that searches can be updated). When you insert a View using the Fields and Views button you are asked whether to display on the form or both, it should be fairly self-explanatory.
Otherwise you would need to add some custom code.
Try adding the following to your theme's functions.php file:
/** * No initial results * * Don't show View results until a filter has been applied * * Tests for custom field filters, taxonomy filters, or text searches */ function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){ $target_views = array( 226 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { // is there a filter set? if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) { $query_results->posts = array(); $query_results->post_count = 0; } } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
This topic contains 1 reply, has 2 voices.
Last updated by 6 years, 9 months ago.
Assisted by: Nigel.