Joe H.
Support threads created in the last 30 days: 0
Favorite Forum Topics
Status | Topic | Supporter | Voices | Posts | Freshness |
---|---|---|---|---|---|
How To Require User to Select At Least One Parameter in Custom Search
Started by: julieP
in: Toolset Professional Support
Problem: I have a custom search View that includes two taxonomy filter controls and a text search field. Now, the View shows all results at first. I would like to hide all the results until the user selects at least one taxonomy or searches for some specific text. Here are the current filter controls: [wpv-filter-start hide="false"] [wpv-filter-controls] <div class="form-group"> <label>[wpml-string context="wpv-views"]Select A Category[/wpml-string]</label> [wpv-control-post-taxonomy taxonomy="category" type="select" url_param="wpv-listing-category"] </div> <div class="form-group"> <label>[wpml-string context="wpv-views"]Select A Location[/wpml-string]</label> [wpv-control-post-taxonomy taxonomy="location" type="select" url_param="wpv-location"] </div>[/wpv-filter-controls] [wpv-filter-end] Solution: Ensure your View's Filter Query section is configured appropriately for this search. It should include both taxonomy filters, as well as a text search filter. See the screenshot attached for the required configurations. Next, add the following code to functions.php: add_filter( 'wpv_filter_query', 'drop_empty_search_query', 10, 3 ); function drop_empty_search_query( $query_args, $view_settings, $view_id ) { $ids = array(1, 2, 3); if (in_array($view_id, $ids)){ if ( // taxonomy filter needs to check for not '0' as well as not empty ( isset($_GET['wpv-listing-category']) && $_GET['wpv-listing-category'] != '0' ) || // taxonomy filter needs to check for not '0' as well as not empty ( isset($_GET['wpv-location']) && $_GET['wpv-location'] != '0' ) || // text search only needs to check for not empty ( isset($_GET['wpv_post_search']) && $_GET['wpv_post_search'] != '' ) ) { } else { $query_args['post__in'] = array(0); } } return $query_args; } Replace 1, 2, 3 with your View ID or a comma-separated list of multiple View IDs where you want to apply this custom query filter. Relevant Documentation: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query |
3 | 13 | 6 years, 11 months ago |