Skip Navigation

[Resolved] Search on term field

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

Problem: I have a text search form on my View, and I would like to display a message in the results area until the User submits a search term. If no results are found after searching, I would like to display a different message. If results are found, I would like to display the results.

Solution: Use the wpv_filter_query_post_process filter to suppress results until the wpv_post_search parameter is supplied.

add_filter( 'wpv_filter_query_post_process', 'drop_empty_textsearch_query', 10, 3 );
function drop_empty_textsearch_query( $query, $view_settings, $view_id ) {
    $ids = array(1, 2, 3, 4);
    if (in_array($view_id, $ids)){
      if ( !isset($_GET['wpv_post_search']) ) {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Add the wpv_post_param shortcode to your theme's functions.php file to access URL parameters:

add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );
 
function wpv_post_param_shortcode( $atts ) {
  if ( !empty( $atts['var'] ) ) {
    $var = (array)$_GET[$atts['var']];
    return esc_html( implode( ', ', $var ) );
  }
}

In your View's Loop Output editor, create your results design inside the wpv-loop tags. In the "no results found" area, use conditional HTML and your new wpv-post-param shortcode to determine if the wpv_post_search parameter exists, and display different messages.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

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

Last updated by Pat 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#589172

Pat

Hello,

I'm working on a WC site and have created some term fields for product taxonomy.
One of these term field is a reference (single line field) that is linked to the taxonomy (my client prefers to have a reference than a taxonomy term). Until now, no issue.
What I need now is to insert a search box in the site and gives opportunity to users to enter the reference of the product taxonomy. As soon as the reference is recognized, the user should be redirected to the taxonomy page.
How can I do this with Toolset?
Regards
Pat

#589250

Well there are two types of forms in Toolset. There are CRED forms, and there are Views search forms. A CRED form doesn't make sense here, since you're not editing or creating a post. So the only other option is a View search form, but, there's not a way to force the redirect in Toolset. So I think the best way to do this is with a generic form you create yourself. Use JavaScript to modify the "action" attribute to point to the correct URL when the form is submitted:

<form action="#" id="custom-tax-reference-search" method="post">
<input id="tax-reference" type="text" />
<input type="submit" value="Search" />
</form>
jQuery(document).ready(function(){
  jQuery('#custom-tax-reference-search').submit(function(e) {
    var url = '/tax-slug/' + jQuery('#tax-reference').val();
    jQuery('#custom-tax-reference-search').attr('action', url);
  });
});
#589347

Pat

Hi Christian,

Thanks for your answer.
One thing I have difficulty to understand is how I can define specific url depending on the input in the search box?
Let me know
Regards
Pat

#589539

Hi Pat, this code redirects the User using whatever they type in the search box:

jQuery(document).ready(function(){
  jQuery('#custom-tax-reference-search').submit(function(e) {
    var url = '/tax-slug/' + jQuery('#tax-reference').val();
    jQuery('#custom-tax-reference-search').attr('action', url);
  });
});

If you need to search for term matching this custom field value, you could change the destination URL to point to a custom search form and pass in a URL parameter:

<em><u>hidden link</u></em>

Create a custom page called your-search-form-page and insert a Taxonomy View filtered by your term field, using the wpvtermfield parameter. In the Loop Output editor, insert another View of posts filtered by taxonomy term, where the term is set by the parent taxonomy View. This will give you an archive of posts assigned to this term. Or, in the Loop Output editor, insert a link to the taxonomy archive.

#590142

Pat

Hi Christian and thanks for your explanation.

In the meantime, I have tested another way and now uses a specific postype to manage all the references. This means I'm able to use the Views search box (which is not possible when you are working with taxonomies).

So, I have placed the search views on a page with the search result in the same page.
The issue I have is that I would like the search result not to show results until the user have entered his text on the search box and have validated the search.
Is there a way to do this?
Regards
Pat

#590307

Yes, you can do that with some custom code utilizing wpv_filter_query_post_process:

add_filter( 'wpv_filter_query_post_process', 'drop_empty_textsearch_query', 10, 3 );
function drop_empty_textsearch_query( $query, $view_settings, $view_id ) {
    $ids = array(1, 2, 3, 4);
    if (in_array($view_id, $ids)){
      if ( !isset($_GET['wpv_post_search']) ) {
        $query->posts = array();
        $query->found_posts = 0;
        $query->post_count = 0;
      }
    }
    return $query;
}

Replace 1,2,3,4 with a comma-separated list of View IDs where you want the results to be empty until the text search field is updated. More info about this filter here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

#590600

Pat

Hi Christian,

Perfect, many thanks.
A last little point : is it possible to have a different message for each of the following cases :

1. First time the search Views is displayed (nothing has been entered into the search box)
2. A text has been entered in the search box, but the result is not valid (no post with this title). Normally, this is covered by the [wpv-no-items-found] shortcode, but I need something else (or a specific conditional output?) for the first case.

Regards
Pat

#590979

You can use conditional HTML inside the wpv-no-items-found shortcode, so you could test the value of the URL parameter 'wpv_post_search'. If it doesn't exist or equals an empty string, show message #1. If it exists, show message #2.

This custom shortcode can be used to determine the value of any URL parameter using PHP:

add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );

function wpv_post_param_shortcode( $atts ) {
  if ( !empty( $atts['var'] ) ) {
    $var = (array)$_GET[$atts['var']];
    return esc_html( implode( ', ', $var ) );
  }
}
[wpv-post-param var="wpv_post_search"]

Be sure to register wpv-post-param in Toolset > Settings > Frontend content > 3rd party shortcode arguments.

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

#591311

Pat

Hi Christian,

That's perfect.
Many thanks
Pat