Skip Navigation

[Resolved] Search view results

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

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 9 replies, has 2 voices.

Last updated by avansisI-2 4 years ago.

Assisted by: Jamal.

Author
Posts
#1892181

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?

#1893153

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.

#1893217

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 );
#1893249

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. **

#1893269
#1893295

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.

#1893371

Of course Jamal.

Thanks

#1894987

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.

#1895497
Captura de pantalla 2021-01-05 a las 22.35.31.png

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

#1898579

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;
}
#1899341

My issue is resolved now. Thank you!