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.
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.
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.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.
Hello, please considerer answering my question. Thank you very much. Have a good day.
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.
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.
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?
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!