Merkucio
Merkucio

Reputation: 175

Wpml and Relevanssi - search results in default language only

I have multi-language web store and use WPML plugin with Relevanssi better search plugin together.

Relevanssi plugin has an option

Limit results to current language: If this option is checked, Relevanssi will only return results in the current active language. Otherwise results will include posts in every language.

The problem is that, after I check this option, Relevanssi better search plugin returns results not in the current active language, but in default language only (English in my case). And if I don't check this option, Relevanssi better search plugin returns results in All 3 language!

This is the Relevanssi wpml filter code. Maybe someone knows what to do with this code to get the results in the current active language?!

Relevanssi author has no time to do this :(

function relevanssi_wpml_filter($data) {
$use_filter = get_option('relevanssi_wpml_only_current');
if ('on' == $use_filter) {
    //save current blog language
    $lang = get_bloginfo('language');
    $filtered_hits = array();
    foreach ($data[0] as $hit) {
        if (isset($hit->blog_id)) {
            switch_to_blog($hit->blog_id);
        }
        global $sitepress;

        if (function_exists('icl_object_id') && !function_exists('pll_is_translated_post_type')) {
            if ($sitepress->is_translated_post_type($hit->post_type)) {
                if ($hit->ID == icl_object_id($hit->ID, $hit->post_type, false, $sitepress->get_current_language())) $filtered_hits[] = $hit;
            }
            else {
                $filtered_hits[] = $hit;
            }
        }
        elseif (function_exists('icl_object_id') && function_exists('pll_is_translated_post_type')) {
            if (pll_is_translated_post_type($hit->post_type)) {
                global $polylang;
                if ($polylang->model->get_post_language($hit->ID)->slug == ICL_LANGUAGE_CODE) {
                    $filtered_hits[] = $hit;
                }
                else if ($hit->ID == icl_object_id($hit->ID, $hit->post_type, false, ICL_LANGUAGE_CODE)) {
                    $filtered_hits[] = $hit;
                }
            }
            else {
                $filtered_hits[] = $hit;
            }
        }

Upvotes: 1

Views: 674

Answers (1)

Dario Oddenino
Dario Oddenino

Reputation: 1304

Change icl_object_id to wpml_object_id_filter.

It should become like this:

        if (function_exists('wpml_object_id_filter') && !function_exists('pll_is_translated_post_type')) {
            if ($sitepress->is_translated_post_type($hit->post_type)) {
                if ($hit->ID == wpml_object_id_filter($hit->ID, $hit->post_type, false, $sitepress->get_current_language())) $filtered_hits[] = $hit;
            }
            else {
                $filtered_hits[] = $hit;
            }
        }
        elseif (function_exists('wpml_object_id_filter') && function_exists('pll_is_translated_post_type')) {
            if (pll_is_translated_post_type($hit->post_type)) {
                if (PLL()->model->get_post_language($hit->ID)->slug == ICL_LANGUAGE_CODE) {
                    $filtered_hits[] = $hit;
                }
                else if ($hit->ID == wpml_object_id_filter($hit->ID, $hit->post_type, false, ICL_LANGUAGE_CODE)) {
                    $filtered_hits[] = $hit;
                }
            }
            else {
                $filtered_hits[] = $hit;
            }
        }

Upvotes: 0

Related Questions