Skip to main contentSkip to footer

By default, Relevanssi indexes the different language versions of the posts separately. If your site is in French and English, searching for the English text will find the English posts and searching for the French text will find the French posts. But what if you want the French search terms to find the English post on the English side of the site?

The solution is to adjust the indexing so that Relevanssi indexes the content of both versions for each post. Add this function to your site to achieve that:

add_filter( 'relevanssi_content_to_index', 'rlv_polylang_sync', 10, 2 );
function rlv_polylang_sync( $content, $post ) {
  $language = pll_current_language() === 'en' ? 'fr' : 'en'; // Adjust language codes if necessary
  $translated_post_id = pll_get_post( $post->ID, $language );
  if ( $translated_post_id ) {
    $translated_post = get_post( $translated_post_id );
    $content        .= ' ' . $translated_post->post_content;
    $content        .= ' ' . $translated_post->post_title;
  }
  return $content;
}

This function will index the post content and titles for both languages for each post. If your language pairs are not English and French (en and fr), adjust the language codes in the function. You can also include other parts of the post beside the content and the title.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Showing only one recurring event
Some event calendar plugins do recurring events by creating many posts. That’s fine until those cloned posts fill up your…
Wildcard matching
Relevanssi Premium 2.10.2 introduces a new way to search: wildcard matching. Once the wildcard matching is enabled (it is disabled…

Related Posts:

Comment Section:

8 Comments. Leave new

  • Rodrigo Zamorano
    October 14, 2024 4:34 am

    Hello again. I have tried this just with the exact code in the example, but it makes nothing in my site. My site has english and french post but when I search it only gives the results in the language I’m using. I tried rebuilding the index but the same happens. I’m using Code Snippets and the last versions of WordPress and Relevanssi. What else could I try to find the problem? Thank you very much.

    Reply
    • Check the Relevanssi debugging tools. If the code works correctly, Relevanssi should have indexed both the English and French content for the posts. Has it?

      Reply
      • Rodrigo Zamorano
        October 14, 2024 3:33 pm

        Thank you very much. Finally I understood how it works. I have to put one line for each direction, if needed, like:
        $language = pll_current_language() === ‘en’ ? ‘fr’ : ‘en’;
        $language = pll_current_language() === ‘fr’ ? ‘en’ : ‘fr’;

        And then rebuild the the index. Thanks again!

        Reply
  • Rodrigo Zamorano
    August 29, 2024 12:10 am

    Hello is this option the same that the one that appears on the Relevansi settings and it is called Polylang?

    Anyway, what if I want this but applied only to one type of post? How can I do this?

    Thank you.

    Reply
    • No, this is different. This function makes Relevanssi index both languages for the same post. If the “show all languages” setting is enabled and you search for French words in the English site, you’ll get the French post. With this function, searching with French words returns the English post (and if “show all languages” is enabled, you’ll get both the French and English post).

      You can restrict this function to a single post type. You can throw in a check for the post type:

      if ( 'post_type_foo' != $post->post_type ) {
      return $content;
      }

      Add that as the first thing inside the function, and this will only apply to the post_type_foo post type.

      Reply
      • Rodrigo Zamorano
        August 29, 2024 11:46 pm

        Thank you for your kind answer. The thing is that my site has 5 languages and I want to control that if a person search for documents to download (post type = downloads) it shows the files available disregards the language. I think it would be fair to pay for this kind of support. Thanks again.

        Reply
        • Rodrigo Zamorano
          September 3, 2024 2:54 pm

          Hello, please considerer answering my question. Thank you very much. Have a good day.

          Reply
          • Sorry, I didn’t realize there was a question in your follow-up comment. I thought I already answered you – based on what you say, my answer is exactly the thing you need. Use the function on the page and restrict it to the one post type as I described.

Leave a Reply to Rodrigo Zamorano Cancel reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed