Skip Navigation

[Resolved] Hide search results until user selects filter, and show only the available inputs

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a custom search View with multiple taxonomy filters. I would like to show the results "empty" until the User selects a filter. I also would like to use the Advanced Option "Only show available inputs".

Solution:
Use the following custom filter code:

add_filter( 'wpv_filter_query_post_process', 'drop_empty_search_query_post_process', 10, 3 );
function drop_empty_search_query_post_process( $query, $view_settings, $view_id ) {
    $ids = array(10243);
    if (in_array($view_id, $ids)){
      if (
        // taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_area']) && $_GET['wpv-ccb_core_group_area'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_day']) && $_GET['wpv-ccb_core_group_day'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_time']) && $_GET['wpv-ccb_core_group_time'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_type']) && $_GET['wpv-ccb_core_group_type'] != '0' )
||
// initial load fix for "only show available inputs", this input does not exist
( isset($_GET['wpv_update_check']) )
) {
      } else {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Joe H. 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#606031

I found this post:
https://toolset.com/forums/topic/how-to-require-user-to-select-at-least-one-parameter-in-custom-search/

that seems to offer a solution for what I want to do: hide all search results until/unless a taxonomy value is chosen (on a page where filter and results are on same page).

My page with the filter and results is
hidden link

The URL of my View is (note view ID):
hidden link

I have this in my filter editor for the View:
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="form-group col-sm-4">
<label>[wpml-string context="wpv-views"]Areas[/wpml-string]</label>
[wpv-control-post-taxonomy taxonomy="ccb_core_group_area" type="select" url_param="wpv-ccb_core_group_area"]
</div>
<div class="form-group col-sm-4">
<label>[wpml-string context="wpv-views"]Days[/wpml-string]</label>
[wpv-control-post-taxonomy taxonomy="ccb_core_group_day" type="select" url_param="wpv-ccb_core_group_day"]
</div>
<div class="form-group col-sm-4">
<label>[wpml-string context="wpv-views"]Times[/wpml-string]</label>
[wpv-control-post-taxonomy taxonomy="ccb_core_group_time" type="select" url_param="wpv-ccb_core_group_time"]
</div>
<div class="form-group col-sm-4">
<label>[wpml-string context="wpv-views"]Types[/wpml-string]</label>
[wpv-control-post-taxonomy taxonomy="ccb_core_group_type" type="select" url_param="wpv-ccb_core_group_type"]
</div>
[/wpv-filter-controls]
[wpv-filter-end]

And I have this in my functions.php:

// Toolset: Hides Group Finder search results until a search field is selected by user
add_filter( 'wpv_filter_query', 'drop_empty_search_query', 10, 3 );
function drop_empty_search_query( $query_args, $view_settings, $view_id ) {
$ids = array(10243);
if (in_array($view_id, $ids)){
if (
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_area']) && $_GET['wpv-ccb_core_group_area'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_day']) && $_GET['wpv-ccb_core_group_day'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_time']) && $_GET['wpv-ccb_core_group_time'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_type']) && $_GET['wpv-ccb_core_group_type'] != '0' )
) {
} else {
$query_args['post__in'] = array(0);
}
}
return $query_args;
}

But it does not work. The search fields no longer display the dropdown choices. Since I am adapting code from another post and am not a programmer I probably got something wrong but I dont see it.
Thank you for your help

#606099

It appears that you have the option "Only show available inputs" selected for your View in the custom search Advanced Options, which is incompatible with that code. This alternate approach works better when you want to combine "show only available inputs" with empty results until an option is selected:

add_filter( 'wpv_filter_query_post_process', 'drop_empty_search_query_post_process', 10, 3 );
function drop_empty_search_query_post_process( $query, $view_settings, $view_id ) {
    $ids = array(10243);
    if (in_array($view_id, $ids)){
      if (
        // taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_area']) && $_GET['wpv-ccb_core_group_area'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_day']) && $_GET['wpv-ccb_core_group_day'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_time']) && $_GET['wpv-ccb_core_group_time'] != '0' )
||
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-ccb_core_group_type']) && $_GET['wpv-ccb_core_group_type'] != '0' )
||
// initial load fix for "only show available inputs", this input does not exist
( isset($_GET['wpv_update_check']) )
) {
      } else {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Let me know if this does not work as expected.

#606109

Yes it now works as desired. I never would have figured that out on my own. Thank you Christian.