Hello,
I have a search view that results in results. I opened a ticket a long time ago.
https://toolset.com/forums/topic/create-two-views-with-same-content/
I have noticed that when I search in the search box with no content I get all the results. But I need only get results if I put the exact reference. How can I do this?
Hello and thank you for contacting the Toolset support.
There is no built-in solution for this case. But we can achieve this using custom code. Check this similar ticket and let me know if you need further help with the implementation https://toolset.com/forums/topic/how-to-show-no-initial-results-on-a-view-until-a-filter-is-set/
I hope this helps. I'll remain at your disposal.
Thanks Jamal,
there are a lot of custom codes on that ticket, can you guide me? This is the last code but, maybe are not correct because I dont have any filter, only a search placeholder.
/**
* 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;
$query_results->found_posts = 0;
}
}
return $query_results;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
This will depend on how you are filtering the view. The above example checks for a taxonomy search and a text search. It will also depend on the view's ID. I'll suggest that you share credentials to your website, let me know where the view is used and I'll check it further. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
I have tried different custom codes, but none of them was working for me. To better debug this, I'll need to take a copy of your website and analyze it locally. Let me know if you would agree.
I was able to reproduce the issue locally. But once I completely deactivated the cache, it worked.
I got back to your website and I flushed the cache here hidden link
It worked. As you can see here hidden link
The final code that I used is:
add_filter( 'wpv_filter_query', 'default_blank_func', 10, 3 );
function default_blank_func( $query_args, $view_settings, $view_id ) {
$target_views = array( 7706 ); // Edit to add IDs of Views to add this to
if ( in_array( $view_id, $target_views ) ) {
if ( !isset($_GET['wpv-wpcf-referencia-del-bono']) ) {
$query_args['post__in'] = array(0);
}
}
return $query_args;
}
If the bono's reference is not supplied we instruct WordPress to look for posts that have the ID 0. No post exists with that ID.
Hi Jamal,
I show this in resultados-validador-bonos-negocios/ but in validador-de-bonos-negocio/ when clic en search without any text
keep showing 'bonos'
Attach an image
Yes, you are right. I had a mistake in the custom code. It only checks for the query parameter but it did not check if it holds a value. I updated the code on line 6 to fix it. The current code is:
add_filter( 'wpv_filter_query', 'default_blank_func', 10, 3 );
function default_blank_func( $query_args, $view_settings, $view_id ) {
$target_views = array( 7706 ); // Edit to add IDs of Views to add this to
if ( in_array( $view_id, $target_views ) ) {
if ( !isset($_GET['wpv-wpcf-referencia-del-bono']) || strlen($_GET['wpv-wpcf-referencia-del-bono']) == 0 ) {
$query_args['post__in'] = array(0);
}
}
return $query_args;
}
My issue is resolved now. Thank you!