Posted on

Polylang: Bilingual search

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.

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 *