Skip Navigation

[Resolved] Hiding all search results content until filtered

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by garyC 5 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1219510

I touched on this problem with Christian Cox in a previous support ticket, but I'm struggling to work out the solution and need a little more help. I am building a tour guide site, and have created a CPT for Destinations. There are 3 Taxonomies, within this, UK, Europe and Rest of World. I have a page for each of these taxonomies, where I have placed a filterable View. The only filter in the search form is 'Choose a Country', and I have set this up and it works fine. However, the client wants to hide all the search results by default, and only have the filtered results displayed once a country is selected from the form dropdown and the search button clicked.

Christian offered up this info, but I'm not too confident on which part of the function, in the examples, is the part that hides the content until actioned? Could you give me more of an idea of what these functions do? Here is what he presented to me:

If you're comfortable writing code, you can check these examples:
https://toolset.com/forums/topic/troubleshoot-wpv_filter_query-in-functions-php/
https://toolset.com/forums/topic/displaying-posts-according-to-their-parent-posts/
And our API documentation here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

#1219695

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Gary

This comes up often enough that I have a generic solution you should be able to use.

Add this code as a snippet at Toolset > Settings > Custom Code (don't forget to activate it, run everywhere).

You just need to change the ID of the target views (226 in the sample code) to match the View you want to be affected. (You can add multiple Views using that array if needed.)

The View won't show any results unless one of a custom field filter, taxonomy filter, or text search has been entered.

/**
 * No initial results
 *
 * Don't show View results until a filter has been applied
 * 
 * Tests for custom field filters, taxonomy filters, or text searches
 */
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){
  
	$target_views = array( 226 ); // Edit to add IDs of Views to add this to

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

My issue is resolved now. Thank you Nigel!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.