hey,
is it possible for toolset search to show not exact results but instead show relevant results?
for example: if i am building a recipes site, and the search includes ingredients, kitchen type, etc - can i get partial results (for example, with 2 out of 3 ingredients, but in the same kitchen type etc) and show the percentage of relevance?
thx!
Hi Ido,
Thank you for contacting us and I'd be happy to assist.
The text search feature in Toolset Views uses the WordPress default search, which can only look through the post titles and post content.
To extend that text search to also look through post's custom fields and taxonomy terms, you can use the third-party plugin "Relevanssi" ( https://wordpress.org/plugins/relevanssi/ ).
Here is a guide on using the Relevanssi and the Toolset together:
https://toolset.com/documentation/user-guides/searching-texts-custom-fields-views-relevanssi/
If you don't specifically require a text search, you can alternatively add search fields for custom fields and taxonomies where multiple selections are possible. In the query filter, you can set the comparison to bring in results, which have any of the selected terms or custom fields.
And to show the percentage, you can show the calculated number in each loop's result, based on how many of those selected terms or custom fields are present in the current post.
I hope this helps.
regards,
Waqar
Hey Waqar! How are you doing? 🙂
Thanks - I know about relevanssi and have used it many times - but, as you mentioned, it's a solution for text searches only.
Your suggestion about "any" is interesting - thanks!
I'm not sure though how to get the percentage right. Since we are using multiple selections, which of them should be calculated and how? For example - if we searched for "pears" and "bananas", and the result shows 3 recipes with pears, 2 with bananas, and 4 with both - how would the percentage "score" go?
I guess the formula should be:
1. X is the number of custom fields involved in the search
2. each field's percentage is 100% / X
3. Y is the number or overall appearances of all fields in a single result
4. score is X*Y
Am I right?
Is there a way to get this done by toolset shortcodes? Or would I need jQuery? A filter function?
Thanks!
Hi Ido,
Thanks for writing back and you've got this right.
A simpler way to achieve this would be to register a custom shortcode ( ref: https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/ ) and place it in your result loop in the view.
That shortcode can get the searched terms (X from your message) through the URL parameter using the Super global variable "$_GET" ( ref: hidden link ).
And for the current post's attached terms (Y from your message) you can use Toolset Fields API ( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ ) if it is a custom field or "wpv-post-taxonomy" shortcode ( ref: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy ) if its a taxonomy.
Once you have both X and Y, you can do the processing based on your formula and show the percentage.
regards,
Waqar
Thx Waqar!
This is a bit beyond me. I would greatly appreciate it if you can assist me here, or at least give me some tips as to how to generate this shortcode.
Cheers,
Ido
Hi Ido,
This revolves more around WordPress Shortcodes API ( ref: https://codex.wordpress.org/Shortcode_API ) and PHP code in general and than specifically to Toolset features.
Still, if you could share an example of the view with search filters and the results that you're planning, I'll be in a better position to share some more specific pointers.
For more personalized assistance around custom code, you can always consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
And for community-based assistance related to WordPress and PHP code, these forums are also very useful:
https://wordpress.org/support/forums/
https://wordpress.stackexchange.com/
You're welcome to share the link to a page with a working view through this ticket or a new one.
regards,
Waqar
Thanks Waqar!
I wipped a quick view that you can play with here:
hidden link
no design, just a couple of recipes (results are just showing titles).
filters are checkboxes of "main ingredients", "liquid" (they are recipes for shakes, supposedly) and "extras".
How would I go about adding the percentage shortcode?
Much appreciated!!
Ido
Hi Ido,
Thank you for sharing the link to a page with example search filters.
I'll be performing some tests on my own website with a similar setup and will update you with my findings.
regards,
Waqar
Hi Ido,
Thank you for waiting.
After reviewing your example and testing it on my own website, I'm able to share an example of a custom shortcode that you're looking for:
add_shortcode( 'show_results_relevance', 'show_results_relevance_func');
function show_results_relevance_func($atts) {
// get the searched terms for main ingredients
$searched_recipe_main_ingredient = do_shortcode('[wpv-search-term param="wpv-wpcf-recipe-main-ingredient"]');
if(!empty($searched_recipe_main_ingredient))
{
$searched_recipe_main_ingredient_arr = explode(", ", $searched_recipe_main_ingredient);
// count of searched main ingredients
$searched_recipe_main_ingredient_count = count($searched_recipe_main_ingredient_arr);
// get attached main ingredients for current post
$attached_main_ingredients = do_shortcode("[types field='recipe-main-ingredient' separator=', '][/types]");
if(!empty($attached_main_ingredients))
{
$attached_main_ingredients_arr = explode(", ", $attached_main_ingredients);
// count of common ingredients which were searched and are also attached
$matched_main_ingredients = count(array_intersect($searched_recipe_main_ingredient_arr,$attached_main_ingredients_arr));
}
else
{
$matched_main_ingredients = 0;
}
}
else
{
$searched_recipe_main_ingredient_count = 0;
$matched_main_ingredients = 0;
}
......
}
Notes:
1. This example uses a checkboxes type field with slug "recipe-main-ingredient".
2. As explained in the comments, shortcode first gets the searched terms for this field and then the attached terms and then saves count of searched terms in $searched_recipe_main_ingredient_count and matched terms in $matched_main_ingredients.
3. Once you've repeated the code blocks for other fields, you can do the percentage calculation as needed.
4. For this calculation to work, its important that title and the value to store in the checkboxes options are the same.
( screenshot: hidden link )
regards,
Waqar
thx Waqar!
This looks great, the only issue is that it's not a custom field, but a taxonomy. The reason I'm using a taxonomy and not a custom field is that I have a bulk of recipes and I won't be able to import custom field checkboxes in a bulk, but I will be able to do so in taxonomies.
Will this work with taxonomies too? if I change it to:
param="wpv-recipe-main-ingredient"
Thanks again for all the effort!!
Ido
I tried this with taxonomy "main-ingredient":
add_shortcode( 'show_results_relevance', 'show_results_relevance_func');
function show_results_relevance_func($atts) {
// get the searched terms for main ingredients
$searched_recipe_main_ingredient = do_shortcode('[wpv-search-term param="wpv-main-ingredient"]');
if(!empty($searched_recipe_main_ingredient))
{
$searched_recipe_main_ingredient_arr = explode(", ", $searched_recipe_main_ingredient);
// count of searched main ingredients
$searched_recipe_main_ingredient_count = count($searched_recipe_main_ingredient_arr);
// get attached main ingredients for current post
$attached_main_ingredients = do_shortcode("[wpv-post-taxonomy type='main-ingredient' separator=', ']");
if(!empty($attached_main_ingredients))
{
$attached_main_ingredients_arr = explode(", ", $attached_main_ingredients);
// count of common ingredients which were searched and are also attached
$matched_main_ingredients = count(array_intersect($searched_recipe_main_ingredient_arr,$attached_main_ingredients_arr));
}
else
{
$matched_main_ingredients = 0;
}
}
else
{
$searched_recipe_main_ingredient_count = 0;
$matched_main_ingredients = 0;
}
}
and doesn't work...
wait - if I put the [wpv-search-term param="wpv-main-ingredient"] shortcode, I get a list of searched taxonomies in each recipe. I don't get a count, I get the actual terms, separated with a comma.
But I can't compare the searched ingredients for the ingredients in the recipe and see which of them exists in the search and which not.
Hi Ido,
The basic structure of the custom shortcode will remain the same, whether you use custom fields or taxonomies.
The only difference would be that to get the attached custom fields, you'll use "types" shortcode for fields and for taxonomies "wpv-post-taxonomy" shortcode.
As you noted, "types" fields shortcode, "wpv-post-taxonomy" shortcode and the "wpv-search-term" shortcodes return the results in a comma-separated format.
You'll see that in my example snippet, I've first used "explode" function to convert that format into an array and then used the "count" function to get the count.
I mentioned in note #3 that the shortcode was not complete and it was just shared to give you an idea on how to get the count of attached and the searched terms. Once you've managed to get the values you need, you can perform the calculation of percentage and return it as shortcode's output.
In case you're having difficulty in completing this custom shortcode on your own, I'll recommend hiring a professional more proficient in PHP code as this is already way beyond the scope of support that we can provide over the forum.
I hope you'll understand.
Thx Waqar. I appreciate the help. If I only could see the code working, I would know how to complete it. But it doesn't so I can't begin to tell where to start from.
Right now I have this code:
add_shortcode( 'show_results_relevance', 'show_results_relevance_func');
function show_results_relevance_func($atts) {
// get the searched terms for main ingredients
$searched_recipe_main_ingredient = do_shortcode('[wpv-search-term param="wpv-main-ingredient"]');
if(!empty($searched_recipe_main_ingredient))
{
$searched_recipe_main_ingredient_arr = explode(", ", $searched_recipe_main_ingredient);
// count of searched main ingredients
$searched_recipe_main_ingredient_count = count($searched_recipe_main_ingredient_arr);
// get attached main ingredients for current post
$attached_main_ingredients = do_shortcode("[wpv-post-taxonomy type='main-ingredient' separator=', ']");
if(!empty($attached_main_ingredients))
{
$attached_main_ingredients_arr = explode(", ", $attached_main_ingredients);
// count of common ingredients which were searched and are also attached
$matched_main_ingredients = count(array_intersect($searched_recipe_main_ingredient_arr,$attached_main_ingredients_arr));
}
else
{
$matched_main_ingredients = 0;
}
}
else
{
$searched_recipe_main_ingredient_count = 0;
$matched_main_ingredients = 0;
}
}
I have registered the shortcode [show_results_relevance] - but when I place it in the view, i get nothing.
Just this minor help - and I'd be on my way.
Thanks!
Ok - I'm slowly getting there...
I just need to figure how to display the results in the code.
I tried adding:
add_shortcode( 'show_results_relevance', 'show_results_relevance_func');
function show_results_relevance_func($atts) {
// get the searched terms for main ingredients
$searched_recipe_main_ingredient = do_shortcode('[wpv-search-term param="wpv-main-ingredient"]');
if(!empty($searched_recipe_main_ingredient))
{
$searched_recipe_main_ingredient_arr = explode(", ", $searched_recipe_main_ingredient);
// count of searched main ingredients
$searched_recipe_main_ingredient_count = count($searched_recipe_main_ingredient_arr);
// get attached main ingredients for current post
$attached_main_ingredients = do_shortcode("[wpv-post-taxonomy type='main-ingredient' format='name' separator=', ']");
if(!empty($attached_main_ingredients))
{
$attached_main_ingredients_arr = explode(", ", $attached_main_ingredients);
// count of common ingredients which were searched and are also attached
$matched_main_ingredients = count(array_intersect($searched_recipe_main_ingredient_arr,$attached_main_ingredients_arr));
}
else
{
$matched_main_ingredients = 0;
}
}
else
{
$searched_recipe_main_ingredient_count = 0;
$matched_main_ingredients = 0;
}
return $searched_recipe_main_ingredient_count;
return $matched_main_ingredients;
}
But I got only the first and not the second.
How do I get the number to display in the view?
thanks!!