I am trying to: Find a way to display a list of the taxonomies that matched a particular custom search.
So that the user sees all the listings that matched their search, but within each listing there is a small box that says 'matches: and displays the tags (taxonomy items) that triggered those results.
Hi, I'm not quite clear from your description how the custom search should work. Could you provide an example that explains:
- How does the User perform a custom search? Is there a parametric search filter, or a text input basic search field, or some combination of filters?
- What does the User enter as a filter criteria, is it a taxonomy term name, or a taxonomy name, or a taxonomy term custom field value, or a post title, etc?
- What if the User enters something different in a text search field, like the post title or a taxonomy name instead of what you mentioned in the previous question?
Hey Christian!
Thanks for your help.
Check out the site here: hidden link.
Basically we want to display results in order of BEST MATCH.
And not Filter out the options that don't match, but to just tag the ones that match with the a simple html box that says 'matches: eating disorder, one-on-one support, etc.'.
Does that make sense?
Okay yes, I think it makes a bit more sense now. Unfortunately "best match" is not one of the sorting options provided by WordPress or Toolset, so ordering results using cumulative search term relevance isn't currently possible. It would require a significant amount of custom code, which falls outside the scope of support we offer here in the forums. A skilled developer may be able to set this up, and we offer a portal where you can contact developers who have knowledge of Toolset: https://toolset.com/contractors
Within each search result, you have the ability to use conditional HTML to display different content based on some conditional criteria. We offer the shortcode wpv-search-term, which can be used to access the search criteria that were entered in the filter. So it might be possible to use that in a conditional and display different messages based on the chosen filters. If you want to know which mental health problem the User searched for, the code might look like this:
You searched for mental health problem(s): [wpv-search-term param="wpv-mental-health-problem"]
The param attribute can be used to determine search criteria based on the URL param defined for each filter in the Query Filter or Search and Pagination panel. Combine this information with some conditional HTML, and you might be able to accomplish something similar to what you described in the "Matches:" block of each result.
Let me know if you have additional questions about what can be accomplished in Views.
Here is the documentation for each of those features:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-search-term
Hi!
ok.. I've tried this but it shows all the taxonomies related to the search result, not just the taxonomies that the user selected.
Does that make sense?
We want to only highlight what MATCHES the user's Search.
hidden link
I was thinking you could use conditional HTML to check each term, but now that I'm thinking through it more carefully I think it will require another approach.
- Create a View of the taxonomy that represents mental health issues, and apply a term filter where the term is set by the current post in the loop.
- In the Loop Output editor, insert the term title or term link, depending on what you want to display in the list of matching terms.
- Insert this View in your custom search View in place of the wpv-search-term shortcode.
- This should output a list of all the mental health terms associated with each post. You can add a custom filter that filters those terms using the User's provided search terms, and returns only the matching results. Here is the code you can add to functions.php:
add_filter( 'wpv_filter_taxonomy_post_query', 'toolset_matching_terms_in_url_filter', 20, 4 );
function toolset_matching_terms_in_url_filter( $items, $tax_query_settings, $view_settings, $view_id ) {
$view_ids = array( 12345 );
if (in_array($view_id, $view_ids)){
$urlItems = isset($_GET['wpv-mental-health-problem']) ? $_GET['wpv-mental-health-problem'] : array();
//$items = array_intersect($items, $urlItems);
$items = array_filter($items, function($k) use ($urlItems) {
$slug = $k->slug;
return in_array($slug, $urlItems);
});
}
return $items;
}
Change 12345 to be the numeric ID of the new term View.
We've almost gotten this to work!
But we have multiple taxonomy views. How do we make this work for more than one taxonomy view?
If all the Views use the same URL parameter wpv-mental-health-problem, then you can simply add the View IDs in line #3 above, as a comma-separated string:
$view_ids = array( 12345, 67890 );
If the Views use different URL parameters, I need to know the ID of each View and the URL parameter required for each View.
hey..
No they don't use the same URL parameter.
They use different URL parameters.
If the Views use different URL parameters, I need to know the ID of each View and the URL parameter required for each View.
Hey! I've just opened another support ticket since we're having an issue with different taxonomy's conflicting with 'or'.
So I'm combining them, in which case we would need just one taxonomy to display matches.
I just need to make sure I can display it properly.
Okay I see, keep me posted and let me know if this ticket is effectively resolved, or if you need assistance after combining the taxonomies.