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
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);
});
});
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
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.
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
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
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
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/
Hi Christian,
That's perfect.
Many thanks
Pat