Skip Navigation

[Resolved] Hide search results until user selects filter

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

Problem: I have a custom search View. I would like to hide the results until a User chooses some filter.

Solution: Use custom code to hide all search results until some filter is added.

add_filter( 'wpv_filter_query', 'drop_empty_search_query', 10, 3 );
function drop_empty_search_query( $query_args, $view_settings, $view_id ) {
$ids = array(1, 2, 3);
if (in_array($view_id, $ids)){
if (
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-wpcf-store-name']) && $_GET['wpv-wpcf-store-name'] != '0' )
||
// 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;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 6 years 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.

Our next available supporter will start replying to tickets in about 2.41 hours from now. 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 10 replies, has 2 voices.

Last updated by tohL 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1139458

I have found this solution https://toolset.com/forums/topic/troubleshoot-wpv_filter_query-in-functions-php/

But I'm don't know the beginning part. I'm the beginner for this.

Could you please guide me for this.

Thank you.

#1139505

[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="form-group">
<label>[wpml-string context="wpv-views"]Store Name[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-store-name" type="select" url_param="wpv-wpcf-store-name"]
</div>
[wpv-filter-submit output="bootstrap"]
[wpv-filter-reset output="bootstrap"]
[/wpv-filter-controls]
[wpv-filter-end]

add_filter( 'wpv_filter_query', 'drop_empty_search_query', 10, 3 );
function drop_empty_search_query( $query_args, $view_settings, $view_id ) {
$ids = array(1, 2, 3);
if (in_array($view_id, $ids)){
if (
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-wpcf-store-name']) && $_GET['wpv-wpcf-store-name'] != '0' )
||
// 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;
}

#1139507

I already followed the steps but it still not working. Please help for this.
Thank you.

#1139837

This code goes in your child theme's functions.php file:

add_filter( 'wpv_filter_query', 'drop_empty_search_query', 10, 3 );
function drop_empty_search_query( $query_args, $view_settings, $view_id ) {
$ids = array(1, 2, 3);
if (in_array($view_id, $ids)){
if (
// taxonomy filter needs to check for not '0' as well as not empty
( isset($_GET['wpv-wpcf-store-name']) && $_GET['wpv-wpcf-store-name'] != '0' )
||
// 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;
}

Or you can add it in Toolset > Settings > Custom Code as a new snippet.

You must change 1, 2, 3 in the code here:

$ids = array(1, 2, 3);

Find the numeric ID of this View by going to Toolset > Views. Place that number inside the parentheses like this:

$ids = array(YOUR NUMBER HERE);

If this isn't working, I'll be glad to take a closer look. Please provide login credentials in the private reply fields here.

#1140183

This is the page that I want to show the result.

hidden link

I'm using the elementor to display the result.

#1140185
3.png
2.png
1.png

I have tried to edit and it is work but the drop down list do not show up the list of stores. As shown in 1.png

After I clicked the SELECT LOCATION then only show up all the list in the result. As shown in 2.png and 3.png.

I wish the list can show up in the drop down list and the result is empty before i select any of the data.

#1140507
Screen Shot 2018-11-05 at 11.16.58 AM.png
Screen Shot 2018-11-05 at 11.17.09 AM.png

Hi, I have tested in Chrome, Firefox and Safari, and it seems to be working for me. I have tried as a Guest and as a logged-in Admin, but the results are the same. I see the Stores appear in the dropdown list, and no results are shown when the page first loads. Then I select a store and click "Submit", and the results are displayed. Are you testing in a specific browser? Can you clear your browser cache and try again?

Can you tell me step by step how to reproduce the problem?

Also - I do not recommend using Toolset Layouts with Visual Composer or with Elementor. You should deactivate Layouts if you plan to use another page builder.

#1140936

Hi Christian,

Thank you for your support. The code you sent to me is work but just need to do a setting at Custom Search View.

Custom Search View --> Let me choose individual settings manually

I have a question here. Just now i tried to click submit before select any location then the search result shown me all the data.

Can you help to modify the code so that it show the warning "please select the location"?

Thank you.

#1141438

I don't have any code to help you show a warning, but I can modify the code so that no results will be shown. Change this line:

( isset($_GET['wpv-wpcf-store-name']) && $_GET['wpv-wpcf-store-name'] != '0' )

To this:

( isset($_GET['wpv-wpcf-store-name']) && $_GET['wpv-wpcf-store-name'] != '' && $_GET['wpv-wpcf-store-name'] != '0' )

That should fix the problem so no results are shown if you click Submit before choosing a store name.

#1141884

My issue is resolved now. Thank you Christian.

#1142912

Hi Christian,

I have an issue here. When I use my phone to check this page.

The dropdown list is not function.

Can help to fix it?

Thank you.