Skip Navigation

[Resolved] How to disable the search in view using blank?

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

Problem: I would like to prevent search results from appearing until a custom search filter is added.

Solution: Use our PHP Views Filter API wpv_filter_query_post_process to drop a View's results until some meta_query is added. The following custom code can be used as a template:

/**
 * No initial results until a filter has been applied
 * Tests for one specific custom field search filter
 * Reference: https://toolset.com/forums/topic/how-to-disable-the-search-in-view-using-blank/
 */
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){
 
  $target_views = array( 123, 456 );
  $field_slug = 'field-slug';
 
  // you should not edit below this line
 
  if ( in_array( $view_id, $target_views ) ) {
    // blank search indicates just blank space in the filter
    $blank_search = isset($query_results->query['meta_query']) && sizeof($query_results->query['meta_query']) == 2 && $query_results->query['meta_query'][0]['key'] == 'wpcf-'.$field_slug && trim($query_results->query['meta_query'][0]['value']) == '';
    // if there is no search term set, drop all results
    if ( !isset( $query_results->query['meta_query'] ) || $blank_search ) {
      $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 );

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

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

Last updated by Tai Chang 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#2089515

I create a view with search field using custom post field. I want to ensure the user to enter something to see the search result, and disable the blank (default means search ALL) in search of the view. How can I do it?

#2089609

Hello, I understand you would like to display no search results until your site visitors enter something in a custom field filter. You could do this with a custom code snippet that implements our Views Filter API to display no results until some filter content is applied. This PHP snippet can be added to your child theme's functions.php file, or to a new custom code snippet in Toolset > Settings > Custom Code. If you use Toolset's custom code features, set the snippet to run everywhere and activate the snippet.

I have an example snippet available that will cause the View to display the "No items found" message until the visitor enters some kind of value in the custom field filter:

/**
 * No initial results until a filter has been applied
 * Tests for one specific custom field search filter
 * Reference: https://toolset.com/forums/topic/how-to-disable-the-search-in-view-using-blank/
 */
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){

  $target_views = array( 123, 456 );
  $field_slug = 'field-slug';

  // you should not edit below this line

  if ( in_array( $view_id, $target_views ) ) {
    // blank search indicates just blank space in the filter
    $blank_search = isset($query_results->query['meta_query']) && sizeof($query_results->query['meta_query']) == 2 && $query_results->query['meta_query'][0]['key'] == 'wpcf-'.$field_slug && trim($query_results->query['meta_query'][0]['value']) == '';
    // if there is no search term set, drop all results
    if ( !isset( $query_results->query['meta_query'] ) || $blank_search ) {
      $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 );

You will replace 123, 456 with the numeric ID of the View where you want to apply this filter. If there is more than one View where you want to apply this filter, you can use a comma-separated list of View IDs here instead. You will replace field-slug with the slug of the custom field used for filtering.

Let me know if this code is not working as expected and I can take a closer look.

#2089653

Hi Christian

Thanks for your kind and timely support.

I follow your instruction to add the codes into toolset custom code snippet. It works for the views which I created inside the WordPress pages.

However, it does not work for the WordPress archive of toolset, even though I include the WordPress archives ID into the codes.

Can you further help on the WordPress archive blank search issue? Thanks!

#2089707

Okay I understand, unfortunately the PHP Filters we have available only impact the results of Views. We do not provide a similar filter to perform the same type of adjustments in WP Archives. I believe you would need a custom code solution for archives that falls outside the scope of support we offer here, using the built-in WordPress API pre_get_posts as shown in another example:
https://facetwp.com/how-to-customize-archive-queries/

https://developer.wordpress.org/reference/hooks/pre_get_posts/

#2089735

My issue is resolved now. Thank you!