Use relevanssi_hits_filter
filter hook to go through the posts to pick up all the categories present, store them in a global variable and show that.
Something like this:
add_filter('relevanssi_hits_filter', 'relevanssi_read_categories');
function relevanssi_read_categories($hits) {
global $relevanssi_search_categories;
foreach ($hits[0] as $hit) {
$cats = get_the_category($hit->ID);
foreach ($cats as $cat) {
$relevanssi_search_categories[$cat->name] = true;
}
}
$relevanssi_search_categories = array_keys($relevanssi_search_categories);
sort($relevanssi_search_categories);
return $hits;
}
then on your search results template:
<?php global $relevanssi_search_categories; echo 'Relevanssi found ' . $wp_query->found_posts . ' hits in categories ' . implode(', ', $relevanssi_search_categories) . '.'; ?>
Hey Mikko,
That was exactly what I was looking to do. You nailed it!
One last question about this… How can I show the categories as links?
Thank you for taking the time to go over this issue for me.
Hi Mikko,
I have just found an issue, when I perform a search that gets no hits, I am getting error messages on top of the page:
Warning: array_keys() expects parameter 1 to be array, null given in /home/content/01/9753501/html/wp-content/themes/service/functions.php on line 177
and
Warning: sort() expects parameter 1 to be array, null given in /home/content/01/9753501/html/wp-content/themes/service/functions.php on line 178
Any thoughts?
Here’s a version that fixes the error message and should provide the links: http://pastebin.com/KKmf5BZP
That did the trick!
Thanks of the support
This is solved!
This is great code, but how would one integrate custom taxonomies / custom categories. Been trying to modify the snippet above to use get_the_terms but cant figure it out?
That’s pretty much it. Put the code you currently have in a pastebin and let me take a look at it.
Below is the code I’m using, more or less im trying to mirror the same process but using get_the_terms. It seem to grab the terms but loses the link resulting in a error or something.
http://pastebin.com/wwKcypYf
error given is: Invalid argument supplied for foreach()
reguarding the
foreach ($terms as $term){
thanks for any help!
Wrap the foreach in if (is_array($terms)) { ... }
conditional, as get_the_terms() returns false
if there are no terms, causing the error you’re getting.
Otherwise your code seems correct to me.