Problem: I would like to prevent search results from appearing until a custom search filter is added.
Solution: Use our PHP Views Filter API wpv_filter_query_post_process to drop a View's results until some meta_query is added. The following custom code can be used as a template:
/** * No initial results until a filter has been applied * Tests for one specific custom field search filter * Reference: https://toolset.com/forums/topic/how-to-disable-the-search-in-view-using-blank/ */ function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){ $target_views = array( 123, 456 ); $field_slug = 'field-slug'; // you should not edit below this line if ( in_array( $view_id, $target_views ) ) { // blank search indicates just blank space in the filter $blank_search = isset($query_results->query['meta_query']) && sizeof($query_results->query['meta_query']) == 2 && $query_results->query['meta_query'][0]['key'] == 'wpcf-'.$field_slug && trim($query_results->query['meta_query'][0]['value']) == ''; // if there is no search term set, drop all results if ( !isset( $query_results->query['meta_query'] ) || $blank_search ) { $query_results->posts = array(); $query_results->post_count = 0; $query_results->found_posts = 0; } } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 4 replies, has 2 voices.
Last updated by 3 years, 8 months ago.
Assisted by: Christian Cox.