I touched on this problem with Christian Cox in a previous support ticket, but I'm struggling to work out the solution and need a little more help. I am building a tour guide site, and have created a CPT for Destinations. There are 3 Taxonomies, within this, UK, Europe and Rest of World. I have a page for each of these taxonomies, where I have placed a filterable View. The only filter in the search form is 'Choose a Country', and I have set this up and it works fine. However, the client wants to hide all the search results by default, and only have the filtered results displayed once a country is selected from the form dropdown and the search button clicked.
Christian offered up this info, but I'm not too confident on which part of the function, in the examples, is the part that hides the content until actioned? Could you give me more of an idea of what these functions do? Here is what he presented to me:
This comes up often enough that I have a generic solution you should be able to use.
Add this code as a snippet at Toolset > Settings > Custom Code (don't forget to activate it, run everywhere).
You just need to change the ID of the target views (226 in the sample code) to match the View you want to be affected. (You can add multiple Views using that array if needed.)
The View won't show any results unless one of a custom field filter, taxonomy filter, or text search has been entered.
/**
* 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 ) ) {
// if there is a search term 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;
$query_results->found_posts = 0;
}
}
return $query_results;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );