Skip Navigation

[Closed] hide search results until user enters text and hits submit

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

Last updated by Christian Cox 2 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#2126727

I'm trying to more or less achieve what is described here: https://toolset.com/forums/topic/how-to-use-hide-search-results-until-after-user-selects-filter-by-using-function/ to search a directory of employees by last name. For various reasons I do not want to split the view and have the results display on a different page, which I know would be the easiest solution.

My search is a text box that searches the field wpcf-last. Here is the snippet I am using, but I don't think it's working because this isn't a taxonomy filter, I only want the relevant results to appear after a user types in some text and hits submit.

add_filter( 'wpv_filter_query', 'drop_empty_search_query', 10, 3 );
function drop_empty_search_query( $query_args, $view_settings, $view_id ) {
$ids = array(510);
if (in_array($view_id, $ids)){
if (
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-wpcf-last']) && $_GET['wpv-wpcf-last'] != '' )
||
// text search only needs to check for not empty
( isset($_GET['wpv_post_search']) && $_GET['wpv_post_search'] != '' )
) {
} else {
$query_args['post__in'] = array(0);
}
}
return $query_args;
}

#2127211
Screen Shot 2021-07-27 at 4.50.35 PM.png

Hello, I can provide an updated snippet for you. If there are no other Query Filters applied other than the "Last" custom field filter, you can use the following snippet:

/**
 * 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( 510 ); // Edit to add IDs of Views to filter
    if ( in_array( $view_id, $target_views ) ) {
        // if there is no filter or search term, drop all results
        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 );

Be sure the View ID 510 is accurate. If not, replace it with the ID of the View you want to adjust with this filter, or replace it with a comma-separated list of View IDs if there are multiple Views you wish to filter.

Any additional Query Filters applied to this View will impact the custom code necessary to hide the initial results. For example, if this View is set up to only display posts with a specific taxonomy term, that will require an updated snippet. If so, please toggle open all Query Filters and include screenshots showing all the details of those Query Filters. If you are using the legacy Views editor, you can expose Query Filters by clicking the tab "Screen Options" in the top right corner of the View editor screen. If you are using the Block Editor to create this View, please select the top-level View block in the Block Editor and open the Content Selection panel to find the Query Filters (screenshot attached).

#2127843
Edit-View-Directories.jpg

I added this snippet, the view ID is definitely correct. I'm using the legacy Views editor, here is a screenshot of what it looks like when I expose Query Filters. Search results are still displayed on load instead of hidden.

#2128137

Actually I figured it out with a conditional display of search results:

[wpv-layout-start]
[wpv-items-found]
[wpv-conditional if="( '[wpv-search-term param='wpv-wpcf-last']' ne '' )"]
<!-- wpv-loop-start -->
<wpv-loop>
<p>[types field='first'][/types] [types field='last'][/types]</p>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-conditional]
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

#2128139

But I would still really like to know how to make it work using the snippet!

#2128305

Hmm, I tested a similar View and snippet in my local site and it was working well..."No results found" is shown until some text is entered in the filter and the search form is submitted. Something else must be going on. How exactly did you add the snippet - did you add code to your child theme's functions.php file, or did you use Toolset > Settings > Custom Code to create a new snippet? If you used the Toolset snippet feature, can you take screenshots of the snippet editor screen so I can see all the configurations for the snippet? It should run everywhere, and it should also be activated.

Beyond that, you can try these standard troubleshooting steps:
- Temporarily activate a default theme like Twenty Twenty One
- Temporarily deactivate all other custom code snippets
- Temporarily deactivate all plugins except Types and Views/Blocks

#2128913

Is there a place I can share login credentials for my testing server so you can try the snippet there?

#2128937

Yes I have activated private fields for your next reply. Please provide login credentials in those private fields and also let me know:
- where I can see the View on the front-end of the site (a link or URL).
- where I can find and edit the snippet through wp-admin.
- if it is okay for me to temporarily deactivate other plugins, deactivate other snippets, and activate a default theme during testing.

The topic ‘[Closed] hide search results until user enters text and hits submit’ is closed to new replies.