Skip Navigation

[Resolved] empty search term shows all posts

This support ticket is created 4 years, 11 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by BillD 4 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#1530281

Hi,
The function below, which you helped me implement, shows empty results by default and works well.
However, when I click on submit with no search term in the form all the posts are displayed.
I would like this, simply not to happen. i.e. clicking submit on the empty form does nothing.
Can I add something to this function below to do this?
Any advice you can provide would be much appreciated.
BillD

/*WP TOOLSET blank search results*/
add_filter( 'wpv_filter_query', 'show_empty_results_default_func', 10,2 );
function show_empty_results_default_func( $query_args, $setting ) {
if($setting['view_id'] == 40){

if( !isset($_GET['wpv_view_count'])){
$query_args['post__in'] = array(0);
}

}
return $query_args;
}

#1530687

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bill,

Thank you for getting in touch.
The reason why the search now shows everything after the search button is pressed is because a search is actually perform even though your haven't selected a specific criteria.

Whenever a search is performed then the url parameter "wpv_view_count" is added to the URL. This is the safest parameter to check as it is always added when a search is performed.

Once this url is added then the function will pick this up and then allow the results to be displayed.

Thanks,
Shane

#1530929

Hi Shane,
Thank you for your reply.
It works perfectly as it is, except when a search is done on an empty search form.
I would prefer it NOT to find everything and display hundreds of posts and pages when a user does an empty search.
Is it possible to redirect back to the same page or simply disable the search if a user performs a search with an empty search form?
Any advice would be much appreciated.
BillD

#1531009

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bill,

Could you send me a link to the search page ?

Perhaps I can modify the hook to account for this.

Thanks,
Shane

#1531113

Hi Shane,
I can't get you access right now.

I was trying to add something to your function :
/*WP TOOLSET blank search results*/
add_filter( 'wpv_filter_query', 'show_empty_results_default_func', 10,2 );
function show_empty_results_default_func( $query_args, $setting ) {
if($setting['view_id'] == 40){

if( !isset($_GET['wpv_view_count'])){
$query_args['post__in'] = array(0);
}

}
return $query_args;
}

**************
to get a redirect to the page $target = '/no-results/';
by using something from this below - from a different wp searchform:

add_filter( 'wpv_view_count', 'no_query_redirect' );
function no_query_redirect( $query ) {
if ( empty( $query->query_vars['s'] ) ) {
$target = '/no-results/';

if ( isset( $_REQUEST['search_form'] ) && ('a' == $_REQUEST['search_form'] ) ) {
$target = '/no-results/';
}
wp_safe_redirect( $target );
exit();
}
return $query;
}
************
But I got lost.
Any ideas?
If not I'll get you access over the next few days.
BillD

#1532331

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bill,

What you need to check is if a the URL parameters are set.

Which is this statement here.


if( !isset($_GET['wpv_view_count'])){
$query_args['post__in'] = array(0);
}

An example is

if( !isset($_GET['wpv_view_count']) || !isset($_GET['wpv-my-param'])){
$query_args['post__in'] = array(0);
}

Please let me know if this helps.
Thanks,
Shane

#1534957

My issue is resolved now. Thank you!