Wpml和Relevanssi - 仅以默认语言搜索结果

时间:2016-01-26 12:06:50

标签: php wordpress wpml relevanssi

我有多语言网络商店,并使用WPML插件和Relevanssi更好的搜索插件。

Relevanssi插件有一个选项

  

将结果限制为当前语言:如果选中此选项,   Relevanssi仅返回当前活动语言的结果。   否则结果将包括每种语言的帖子。

问题在于,在我选中此选项后,Relevanssi更好的搜索插件会返回不是当前活动语言的结果,而只返回默认语言(在我的情况下为英语)。如果我不选中此选项,Relevanssi更好的搜索插件会返回所有3种语言的结果!

这是Relevanssi wpml过滤器代码。也许有人知道如何使用这段代码来获得当前活动语言的结果?!

Relevanssi作者没有时间这样做:(

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;
            }
        }

1 个答案:

答案 0 :(得分:0)

icl_object_id更改为wpml_object_id_filter

它应该是这样的:

        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;
            }
        }