Hello, I have been trying to implement a search page on my development site that displays the results of one search box that queries a custom meta field 'luggage-tag-number' and then displays all matching results with map location pins and the resulting custom post type below.
The search and map are working fine, I just want to know if there is a way to initially display nothing underneath in the posts view until a tag has been searched. Currently all the custom posts 'submissions' are displayed before any tag number is entered into the search field.
I am attaching images of the front-end display and the block set in the edit page mode. Let me know if you need anything else. I have the site in maintenance mode while I'm completing it but I can open it up if needed.
Thanks so much for your help!
Kelly
Hi Kelly,
Thank you for contacting us and I'd be happy to assist.
To achieve this, you'll need a custom function attached to the "wpv_filter_query" hook:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
For example:
add_filter( 'wpv_filter_query', 'hide_till_search_custom_func', 99 , 3 );
function hide_till_search_custom_func( $query_args, $view_settings ) {
// skip if blocks edit screen
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return $query_args;
}
// comma-separated list of target view names
$target_views = array('Test view name');
// get current view's name
$current_view_title = get_the_title($view_settings['view_id']);
// process if the current view is one of the target views
if ( ( isset($view_settings['view_id']) && in_array($current_view_title, $target_views)) ) {
if( empty($query_args['meta_query']) ) {
$query_args['post__in'] = array(0);
}
}
return $query_args;
}
Note: please replace 'Test view name' with your actual view's name for which you'd like to hold the results, until search.
Tip: You can also add multiple target view names in a comma-separated list, like this:
$target_views = array('Test view name 1', 'Test view name 2', 'Test view name 3');
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar