Skip Navigation

[Resolved] Making the filter drop downs *Required

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

Problem: I have a custom search View with two control fields. I would like to require the User to select a value in both fields before any results are shown. As it is now, when the page loads all the results are shown. I would like to show no results.

Solution: @e have an API that is helpful for this situation: wpv_filter_query_post_process. This will allow you to intercept the search results and test the query. If either option is blank, or 0, show no results.

// custom filter that shows no search results if either filter is empty
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(12345);
    if (in_array($view_id, $ids)){
      if (
        // supplier type filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-supplier-category']) && $_GET['wpv-supplier-category'] != '0' )
          &&
        // location filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-area-served']) && $_GET['wpv-area-served'] != '0' )
      ) {
      } else {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Replace 12345 with the numeric ID of this View. You can customize the "No results found" message in the View's Loop Output editor panel.

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

This support ticket is created 6 years, 6 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 4 replies, has 2 voices.

Last updated by davidL-10 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#910534

Tell us what you are trying to do?
I have two search fields.
Supplier type and Location.
If both are left blank we get
/supplier-search/?wpv-supplier-category=0&wpv-area-served=0

And this lists Everything. = Meltdown.

I would like the filter option to be required before we can submit s search/filter.

I have tried writing redirects for each but they don't always work.

Is there any documentation that you are following? Nope

Is there a similar example that we can see?
hidden link

What is the link to your site?
hidden link

#910732

Hi, we have an API that is helpful for this situation: wpv_filter_query_post_process. This will allow you to intercept the search results and test the query. If either option is blank, or 0, show no results. Here's an example:

// custom filter that shows no search results if either filter is empty
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(12345);
    if (in_array($view_id, $ids)){
      if (
        // supplier type filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-supplier-category']) && $_GET['wpv-supplier-category'] != '0' )
          &&
        // location filter needs to check for not '0' as well as not empty
        ( isset($_GET['wpv-area-served']) && $_GET['wpv-area-served'] != '0' )
      ) {
      } else {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Replace 12345 with the numeric ID of this View. You can customize the "No results found" message in the View's Loop Output editor panel. More information about this API:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

#910748

Awesome - thanks a mill

#910756
Screen Shot 2018-06-07 at 18.12.27.png
Screen Shot 2018-06-07 at 18.12.15.png

No - Should have tested first.

Not making any difference.

#910782

Oh I see that there is one issue, the example I was working from tested whether either filter was set, but your setup should test whether both filters are set. So the code should use && instead of || between the conditional clauses. That's the only thing that I would change right now:

...
        ( isset($_GET['wpv-supplier-category']) && $_GET['wpv-supplier-category'] != '0' )
          &&
        // location filter needs to check for not '0' as well as not empty
...

If this doesn't resolve the issue, I'll need to take a closer look. I will provide private reply fields here so you can share login credentials.