Skip Navigation

[Resolved] Do not load custom search results until ‘Search’ is pressed

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

Problem:
Custom searches initially display all results. How to change this so that it works like the Google homepage with no initial results until a search is made?

Solution:
There are two ways to do this. The simplest is to split the search form and search results onto separate pages (the search results page will still include the filters so that searches can be updated). When you insert a View using the Fields and Views button you are asked whether to display on the form or both, it should be fairly self-explanatory.

Otherwise you would need to add some custom code.

Try adding the following to your theme's functions.php file:

/**
 * 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 ) ) {
   
        // is there a filter 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;
        }
    }
   
    return $query_results;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
This support ticket is created 6 years, 9 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
- 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+00:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 6 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#621094
Screenshot 2018-03-01 14.04.30.png

Tell us what you are trying to do?
On my home page I have 6 toggles that hide 1 custom search view each. (See screen shot below.) They actually work great. However, the current size of my home page being downloaded is 11MB - yowza. It turns out that the majority of that is the expansion of those searches with their default 'All' being selected for the filters. (There are 750 data elements that may show in one or more of each toggle - basically 70K lines of html code is generated.)

I want to reduce the page load size, duh. An obvious idea is to dynamically do the search using Ajax not at load time, but rather waiting until either the user:
- expands the toggle (desirable, but only if easy to hook into javascript events)
- presses the search button.

I couldn't figure out how to defer the initial search and population.

Is there any documentation that you are following? Custom search, support forms.

Is there a similar example that we can see?

What is the link to your site? hidden link

Thanks!!!!
p

#621278

Nigel
Supporter

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

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

Hi Peter

You want a google home page type solution, where no results are shown until the search is made.

There are two ways to do this. The simplest is to split the search form and search results onto separate pages (the search results page will still include the filters so that searches can be updated). When you insert a View using the Fields and Views button you are asked whether to display on the form or both, it should be fairly self-explanatory.

Otherwise you would need to add some custom code.

Try adding the following to your theme's functions.php file:

/**
 * 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 ) ) {
  
        // is there a filter 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;
        }
    }
  
    return $query_results;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );

You will need to edit the view ID.