I'm building a custom search. Is there a way to prevent any records from rendering in the loop until a filter is selected?
At the moment, all records render when the page containing the custom search view is loaded. I want no records to show on page load. I want the filters to control any content that is fetched for the loop.
Hi, yes you can achieve this by adding a snippet of custom code. You can place this code in a child theme's functions.php file, or you can create a new snippet in Toolset > Settings > Custom Code and add it to the end of the snippet:
/**
* 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( 123, 456 ); // 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 );
Change 123, 456 to be a comma-separated list of View IDs where you want to apply this filter.
Let me know if you have questions about that.