Do not load custom search results until a choice has been made or ‘Search’ is pressed (Toolset)

Author: 

toolset


Description: 

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.




Kind:

PHP, Toolset


PHP:

/**
* 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 );


URL: