Posted on

Related searches feature

John Blackbourn wrote a cool related searches feature for Relevanssi Premium. It’s included from version 1.7.4.

In order to use the related searches, you must have search logging enabled and a good log of past searches. Then add the following code to your search results template in a suitable place:

if ( function_exists( 'relevanssi_related' ) ) {
    '<h3>Related Searches:</h3><ul><li>',

The first parameter is the search query and the last parameter is the number of searches to show. The rest are about the display.

The function looks at the search log and returns queries which have at least one matching token with the current search. Searching for “john” will match both “john blackbourn” and “john smith”. Searching for “john blackbourn” will match “john smith”, but won’t match “john”, because the function doesn’t suggest queries that consist of only one token.

Also, only queries that have at least two hits and have been made at least twice are included.

When you link to your search results pages like this, it’s highly recommended to tell Google not to display them. Either put robots meta field in your search results pages header saying “noindex”, or use robots.txt to prevent bots from accessing your search results pages. Instructions can be found at

21 comments Related searches feature

  1. As Mikko points out, this feature works really well in theory but in practice your site will need a lot of previous search data before it starts showing useful suggestions (or any suggestions at all).

    The function was written more as an excercise for myself rather than a feature that would get used (although I am using on a couple of sites). A better way to determine related search terms would be to parse the content itself and pick out common related terms, but that’s a whole different ball game.

  2. Hi Mikko!
    unfortunately the function doesn’t work..switching on the error logs there is highlighted a SQL Error + a php notice:

    [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE query LIKE ‘%pediaria%’ AND query NOT IN (‘pediaria’, ‘pediaria’) AN’ at line 3]
    SELECT query FROM WHERE query LIKE ‘%pediaria%’ AND query NOT IN (‘pediaria’, ‘pediaria’) AND hits > 1 GROUP BY query HAVING count(query) > 1 ORDER BY count(query) DESC LIMIT 5

    Pediatra is my search key.

    Notice: Undefined index: relevanssi_log_table in /var/www/domini/aslcn2/wp-content/plugins/relevanssi-premium/relevanssi.php on line 1253

    I’m using Relevanssi Premium 1.10.14

    Thanks a lot in advance!

    1. Looks like there’s an error in the code. I’ll get that fixed in the next version, but meanwhile on line 1253 of relevanssi.php file, change

      FROM ” . $relevanssi_variables[‘relevanssi_log_table’] . ”


      FROM ” . $relevanssi_variables[‘log_table’] . ”

      and it should work.

    1. Stefan, you can find the search terms from wp_relevanssi database table. Just sort by the date column and get as many as you wish. You can create a shortcode that displays the search terms in the way you want – but a word of warning, are you sure you want to give your users a way to make whatever filth they want to show up on your site? I’m not sure I’d want that myself.

  3. Thanks for the replay Mikko,

    I was aware of that situation, where users can type anything. This is experiment I am working with friend.

    We are trying to have simply one type of pages, which will be search page by Relevanssi.

    I want to import huge list of keywords or search terms, and display them on the website. Each search term should link to search page normally.

    What would be easiest way to do this without heavy coding, since I am not programmer. I could do changes and edits in files, but not completely from scratch.


    1. Hire a developer to do it? There’s no shortcut here, somebody really has to write the code that fetches the data from the database table. It’s a simple task, so I’m sure you can find a cheap coder somewhere to do that.

    1. Stefan, Relevanssi doesn’t include any caching – it’s not really what Relevanssi does. However, there’s no reason why a cache wouldn’t be able to cache result pages. I don’t know if it works with WP Rocket, and some caches probably won’t cache pages with query variables, but there’s nothing in Relevanssi that prevents caching.

  4. Hey Mikko,

    I am surprised that Relevanssi doesn’t have any widget at all. Not even in premium package. It would be so much useful to have simple widgets where u can adjust what to show.

    Related, Latest Search or Custom (from the list). With some basic settings like number of search terms, order, link/no link …

    I think u have most of the codes over here, someone just need to put it together and implement.

    It would save users lot of time and avoiding editing code, etc. Especially for none-developers, and that is what is WP is mostly about. Visual, DnD.

    Anyways, that is my feedback to you.


    1. Can you guess how much I like creating UI elements =) Creating widgets in particular is a lot of work I don’t enjoy at all. That’s the main reason.

      Also, I’m not really sure what is necessary. There’s not much demand for specific things. Latest searches is asked a bit, but I’m not sure how to do it well: I don’t like how it makes it possible for users to publish just any content whatsoever on your site. Someone could set up a script that searches “BUY VIAGRA FROM BOB NOW” on your site every five minutes, and that’s what’s in your latest searches all the time then. Not really sure I want to enable something like that.

      Related search terms… well, related to what? How can I tell search term A is related to search term B? String proximity matching? Maybe, but that also has all the dangers of dealing with unfiltered user input.

      So, it’s a combination of these things: my reluctance for dealing with the complications of the WP widget code, lack of clear user demand and the difficulties of dealing with unfiltered user input.

  5. Well, you could use stop words, so even if user search Whatver, it will not be displayed.

    Related search based on the code you provided at the top of page or John comment:

    A better way to determine related search terms would be to parse the content itself and pick out common related terms, but that’s a whole different ball game.

    Sometimes people are to lazy or don’t know to communicate, ask for help, suggest, give a feedback. Feedback is really important, and you are now getting it from advanced user.

    I don’t know what is ur focus or plans with Relevanssi, but introducins this features you could use it for growth new user base, getting more exposure by showing how and why u should use Relevanssi etc.

    Think about it as growth strategy as well, not just another feature. But indeed, widget feature is must for almost every WP plugin.

  6. Growth of user base and adding new features is fine, but I’m developing Relevanssi alone, which means there is no time to add each new feature, and at some point getting new users is not a good thing, as more users means more time spent doing customer support and less time spent developing the plugin.

    Thanks for your feedback, though. It is welcome and appreciated, even if it doesn’t automatically lead to new features. These are complicated features you’re asking for here, and building them will take months in the best case (there’s the already-existing related searches feature, yes, but adding it to a widget requires thinking about performance issues, for example ).

    By the way – if there’s a related searches widget, what should it show when it’s displayed on a page that is not a search page? WordPress doesn’t have a built-in method of displaying widgets on just one particular page, so what would a related searches widget show on a non-search page? Any suggestions?

    1. Well, you need to scale your business. That is the obstacle for everyone. Scalling is process involving and training more people and automation, etc.

      If Relevanssi is one of your main business, you should invest in it, so it can return 10x, 100x.

      For example, maybe relevanssi integrate with Sphinx which allow to even better search, with more performance and for the larger sites. Since you stated that it’s getting slow with 30k + posts.

      Answer on your question is simple. You can show most popular searches or latest searches. Or even autohide on non-search pages.

      1. The large-scale searching is a field I’ll leave to others. There are many superior products there already – there’s no way one man can compete with products like Solr, Elastic, Swiftype, Addsearch, Algolia or Lucidworks Site Search.

        It’s also a question of being true to one’s values and doing what gives one pleasure. For me, that’s puzzling out code problems and helping people, not managing a business or getting rich.

  7. It’s not one man job for sure.

    Anyway, I understand your vision and was very plesant suprised for how long you are responding to comments and dedicated you are.

    Relevanssi is great plugin. Seems his role is to replace default WP search and provide better experience.

    1. I tried replicating this problem but couldn’t. One possible explanation is that the query is stored encoded in the database. Is that possible? As you can see from the relevanssi_related() function, the query is retrieved from the database, and the URL is passed through rawurlencode(), and the title goes through esc_attr(). That’s just single encoding, but if the query is already encoded in the database, that would cause a double encoding.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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