Skip Navigation

[Resolved] Don’t show any results until filter is applied

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

Problem: I have a custom search View with several filters. I would like to hide all the results in the View until at least one filter has been applied.

Solution: You can use the following custom code snippet to hide the results until some filter has been applied:

function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){
   
    $target_views = array( 123,456 ); // Comma-separated list of View IDs
 
    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 );

100% of people find this useful.

This support ticket is created 5 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 5 replies, has 3 voices.

Last updated by Christian Cox 4 years, 12 months ago.

Assisted by: Christian Cox.

Author
Posts
#1356279

Tell us what you are trying to do? - I would like only the search/filtering boxes to show on the page and no results until either a text search or taxonomy select list is chosen. Right now, even though I have added in the View as described in the below thread, the full results are still showing.

Also, I don't want to have the option to show All Results. Only filtered/searched ones.

Is there any documentation that you are following?

Is there a similar example that we can see? I found this support thread but it isn't working for me: https://toolset.com/forums/topic/do-not-show-loop-output-until-submit-button-in-filter-editor-is-selected/

I have added in the View like this:

[wpv-form-view name="filterable-documents" target_id="self"]
[wpv-view name="filterable-documents" view_display="layout"]

What is the link to your site? hidden link

Thanks!

#1356327

And if I can ask one other thing. The categories on that page also have sub-categories. If someone does a filter or a search, it would be nice to show something like DOCUMENT CENTRE > MAIN CATEGORY > SUB-CATEGORY by each result so that the end user can be oriented to where they are in the document tree.

#1356451

Hi, if you would like to display no results until some filter is applied, you can add a bit of custom code. Here is a code snippet you can use:

function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){
  
	$target_views = array( 123,456 ); // Comma-separated list of View IDs

    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 );

Replace 123,456 with a comma-separated list of numeric View IDs where you want to apply this filter. Then you can add this code in a child theme's functions.php file, or in a new code snippet in Toolset > Settings > Custom Code.

If someone does a filter or a search, it would be nice to show something like DOCUMENT CENTRE > MAIN CATEGORY > SUB-CATEGORY by each result so that the end user can be oriented to where they are in the document tree.
You can add this information to each result in the View's loop. You can use the wpv-post-taxonomy shortcode to display information about a taxonomy hierarchy. We have documentation about that shortcode available here: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy
If you have questions about that, we can start a new ticket and discuss in more detail.

#1356475

Thank you. I will open another ticket to discuss the displaying of taxonomy.

#1400691

I was also looking for this functionality, and this solution worked. Please consider adding "only show search results after user input" as an option to the view builder.

#1400721

Hi joonaN, please submit your request here to add your vote for this feature: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/