WPML:wp_query() returns 所有语言的帖子,而不仅仅是当前的一种

WPML: wp_query() returns posts in all languages rather than just current one

无论我是在法语版还是英语版的页面上使用它,wp_query() return我的自定义 post 类型在所有语言中,而不仅仅是在当前的。 Get_posts() 也做同样的事情。

当我访问我的法语页面时,我希望他们 return 仅使用当前语言的 CPT。如何实现?

当使用get_posts()时,设置suppress_filtersfalse:

$myPosts = get_posts(array(
    'suppress_filters' => false
));

http://codex.wordpress.org/Function_Reference/get_posts#Parameters

这是我发现使用 WPML 在特定语言上获取 posts 的最佳方式...

在我的例子中,我需要根据特定语言的标题和 return post:

的 ID 找到 post
$lang='en';
$title='The title you are searching!';

    function getWpIdByTitle($title, $lang){
        global $sitepress;
        // WPML Super power language switcher...
        $sitepress->switch_lang( $lang );
        $args = array(
          'title'        => $title,
          'post_type'   => 'your-post-type', // Default: post
          'post_status' => 'publish',
          'suppress_filters' => false,
          'numberposts' => 1
        );
        $wp_query = new WP_Query( $args );
        return $wp_query->post->ID;
    }

您可以使用 $wp_query->post 作为获取的结果,并对标题、内容等进行回显。

这样你就不需要使用

do_action( 'wpml_set_element_language_details', $set_language_args );

连接您的语言 post,

icl_object_id(1,'post',false,ICL_LANGUAGE_CODE);

获取特定语言的 post 的 ID。