Useful functions in Relevanssi

Relevanssi has some useful functions that can be helpful. Going through the source code looking for them is not fun, so here’s a list of all the functions that I think can be helpful used outside Relevanssi code.

These all are available in both free and Premium, unless noted otherwise. When using the functions, it’s always wise to wrap them in function_exists() block to avoid error messages when Relevanssi is for some reason deactivated:

if (function_exists('name_of_the_function')) {
    name_of_the_function($parameter, $parameter);
}

Table of contents

Functions

relevanssi_didyoumean($query, $before, $after, $n = 5)
Use this function to display the “Did you mean” suggestions. The parameters are: $query is the current search query (use get_search_query()), $before is displayed before the suggestions (for example “<p>Did you mean: “), $after is displayed after (for example “</p>”) and $n is the limit of displaying the suggestions (if you leave it empty, the default value is 5, meaning that the suggestions are only displayed when five or less results are found).

relevanssi_do_query($query_object)
You can run the Relevanssi matching engine at will using this function. The function takes a WordPress query object as an argument and returns nothing. If the query object has a search (a search query is found in $query_object->query_vars['s']), $query_object->posts will contain the posts Relevanssi found.

relevanssi_get_term_taxonomy($term_id)
Given a term id, the function returns the taxonomy of the term.

relevanssi_get_the_tags($separator = ‘, ‘)
Returns a list of tags for the current post (so use in the Loop), with highlights applied. The first parameter sets the separator for the tags and defaults to ‘, ‘. See relevanssi_the_tags() for a version that echoes out the tag list.

relevanssi_get_the_title($post_id)
Returns the title for post $post_id. Use this function to show highlighted titles ($post->highlighted_title). If there’s no highlighting in the title, the basic title is displayed. See relevanssi_the_title() for a version that echoes out the title.

relevanssi_highlight_terms($string, $query)
This function will take a string and a search query, and returns the string with all the words of the search query highlighted. The search query will be tokenized, so stopwords and small words are not highlighted. The highlighting method is the one you’ve specified in the Relevanssi settings. You can get the search query from get_search_query() (how pleasantly obvious).

You can use this function to add highlights to wherever you want. For example, if you have tags in your search results template and want them highlighted, simple replace the_tags() with echo relevanssi_highlight_terms(get_the_tag_list('', ', '), get_search_query()); (or just use relevanssi_the_tags()).

relevanssi_the_excerpt()
Used inside the Loop, this function echoes the post excerpt. It doesn’t trigger any filter functions hooked to the_excerpt(), which is why it exists. If using the_excerpt() causes trouble, try this.

relevanssi_the_tags($separator = ‘, ‘, $echo = true)
Prints out a list of tags for the current post (so use in the Loop), with highlights applied. Use this instead of the_tags() on search results template to get highlights to the list of tags.

Both parameters are optional. The first parameter sets the separator for the tags and if the second is set to false, the tag list will be returned, otherwise it’s echoed.

relevanssi_the_title()
Prints out the title for current post. Use this function to show highlighted titles ($post->highlighted_title). If there’s no highlighting in the title, the basic title is displayed. See relevanssi_get_the_title() for a version that doesn’t echo out the title.

25 comments Useful functions in Relevanssi

  1. i can’t get the excerpt to display using get_excerpt or the relevanssi version of get_excerpt. all i get is […] and I’ve tried adding manual excerpts to a few posts tos ee if that would help, but no luck! Any ideas?

      1. No, which baffles me. Right now I’m using a sloppy fix to at least get *something* to show up… but it obviously doesn’t have the nice excerpts with the highlighted search terms like I was hoping for:

        1. I’d check to see if there’s something weird in your search results loop. Does the global $post get set properly? Try a var_dump($post) – do the post objects look good? If Relevanssi is creating excerpts as it should, there should be an excerpt in $post->post_excerpt.

  2. When i add product using php code not from wordpress dashboard. Then relevanssi doesn’t take this product it it’s search util i open the product page from dashboard and click on update button. How can i solve this problem ?
    Thanks,

      1. Thank you for your encouragement, Mikko! The javascript addition has to be placed into the target page, right? Or is there a way to get it into the URL-link that gets me there? That would be fabulous for quotes to certain text passages, but this probably goes beyond Relevassi … Incidentally I still sometimes use “name” in my blooger blog and it keeps working.

        1. In order for the link to work, there would need to be an anchor on the result page. I suppose that’s possible as well, but it’s really up to your theme and not Relevanssi.

          1. Sorry, we are not able to do this, turns out, for “intellectual reasons” (too dumb). Might it not become standard to always move or scroll the result page to the highlight? I see no reason not to always do it. After all, the searcher wants to find exactly what she’s looking for, not the top of a page containing (for him just hopefully) the search clause? Thank you for considering.

          2. It’s not a standard feature, because it’s hard to do (as you’ve noticed) and requires things that can’t really be standardized and need to be done on per site basis.

            I also don’t think it’s always the correct thing to do – in most cases as a user I’d just get annoyed by this. I can use the browser on-page search, if I need to find a part of the page, but in general I’d prefer to read the page from the beginning.

          3. We are in a special situation, perhaps. We have 23 full books, each a single (!) web page, with all the texts of a lifetime of a defunct author (http://www.britting.de/wordpress/saemtliche-werke/). Could you suggest me someone to help us in a paid effort? Private correspondence to Fritz@Joern.De is ok. I guess finding and spotting (scrolling to) must still be less of an effort, than to manually separate the pages into about thousand pieces. (I haven’t found search engines remembering “name” entry points to closer spot the target.) Here a special name marker (like the command to highlight something) would have to be plugged into the WordPress’ output page. (My dream would be browsers that can be told from the outside to search for a specific word in a page, so you could better reference something in a page … )

  3. Hi there! I’m using Relevanssi to boost the search of a wordpress site I’m developing. I managed Relevanssi to get search results for titles, contents, categories and tags. Then I was trying to highlight the terms for each situation. For the tags I saw there is a function called relevanssi_the_tags(), but it’s returning me the whole link of the tags as strings.

    So where there should be only the tag name, such as “Bangu”, the whole link is printed like “<a href=”http://localhost/pos2017/tag/bangu/” rel=”tag”>Bangu</a>“.

    Any clues on how to solve this?

    Cheers,
    Willian.

    1. relevanssi_the_tags() is the equivalent of the WordPress default the_tags(), which does the same thing. If you want just a highlighted list of tags, get the tags somewhere else, put them in a string and then use relevanssi_highlight_terms() on that string.

      This should probably be fairly close to what you want: echo relevanssi_highlight_terms(get_the_tag_list(”, ‘, ‘), get_search_query());

      1. Yes. It worked! Doing this solved the problem:

        $tagList = strip_tags(get_the_tag_list(”,’, ‘,”));

        And then I’ve put $tagList as an argument inside relevanssi_highlight_terms() .

        Thank you so much for your help.

        Cheers! o/

  4. Please guide how can i show the search result if the searched word is in the last of the post.. By default the results starts with the first paragraph. if the word is in the last paragraph it is not shown in the result.

    1. Arlsan, Relevanssi should show the part of the post that has the searched term, whether that is in the first or the last paragraph. If you’re just seeing first paragraphs, make sure you have enabled Relevanssi-generated excerpts. If you have, and you’re still seeing the basic excerpts, make sure your theme is using the_excerpt() to show the excerpts.

    1. Dave, as long as your theme is using the_excerpt() to print out the excerpt, the highlighting should work automatically. If it’s not, you need to edit the search results template (usually search.php).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.