Skip Navigation

[Resolved] No results at first, Google Like Search

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

Problem:
How can we create a Custom Search with Toolset that resimiliates to Google?
There, the results are not listed until you actually search by something.

Solution:
In short it's possible, with different approaches.
There is a full solution explanation here.
https://toolset.com/forums/topic/what-to-show-if-no-url-param-is-defined/#post-618592

This support ticket is created 6 years, 10 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 3 voices.

Last updated by scottL-3 6 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#618347

In a view you can filter posts on URL parameters.
I would like to the option to show the content in the "no posts found" shortcode if the URL parameter is missing on page load.

#618592

By default, a View, or any search in WordPress, when you load it, will display all results.

To display nothing at first and load the results only after the User has actually submitted a search there are several approaches.

The official is to split the View, as described here:
https://toolset.com/documentation/user-guides/front-page-filters/#displaying-custom-search onwards.

The unofficial, provided by Nigel some time back:

When you want a Google-style search where no results are initially shown you can use the following code, which will prevent results being displayed until a filter is applied.

Works with custom field filters, taxonomy filters, and/or text field searches.

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

I do not suggest this approach thou, as it's simply not how it's supposed to work.

The Views have GUI settings for this to work just fine, but it requires that you load a new (results) page.

The "No results found" is triggered *only* if you have no results by the query.
So you would need to fake those to trick out Views and make it display "no items found".
This is more or less what above code does, and I do not recommend this.

However, you are free to use it, if it fits your project better than the official solution.

#921271

I'd like to use tinaH's example here to make an enhancement request for Views.

All of the approaches to having Views not show results until a search is performed without having to use a second page seem to suffer from the same issue. They have the performance hit of running the search first then the results are either hidden and later revealed with javascript or filtered with a backend filter. Either way the search for all results runs needlessly.

We really need to be able to have a search box followed by search results on the same page that shows nothing until the search is executed and that does nothing until the search is executed. It is bad for site performance and server load to have the loop run for all posts of the type in the view before the search has run.

This needs to be optional as many use cases do need all posts to show until they are filtered or searched.

Other use cases (thousands of historic articles, for example) should not and having the results on a separate page is inconsistent with modern web design trends.

#921531

I'll file that request.

But let's look at google, the most famous search form ever.
It starts here:
hidden link (where "tld" is your countries TLD)
If you search for anything, it ... loads the results in a new URL:
hidden link

Notice the "search" after the hidden link?
That is like a page on WordPress, followed by the classic Search URL parameter syntax:
?s=

It is, I would say, the same as loading the results on a new page, as we do suggest in those cases.

However, I filed your request. But I can not guarantee it's implementation.

#921725

Thanks. My use case is different than the original poster's. My need for search is for product photos. There are too many to show them all pre=search but it is part of a page that also shows a gallery of projects (custom posts) with filters. From a user experience perspective, it is awkward to be able to filter the projects on page but have to leave the page to see the photo search results. I suppose I could replicate the entire page on the search results page to solve this but it seems wrong to have to build a page twice but at least I have a way to do this if the developers can't solve this otherwise.

My other use case is in a sidebar where there just isn't room to show all the posts pre-search.