You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现WordPress多语言站点自定义文章类型评论跨语言同步显示

Hey there! Let's fix that comment sync issue for your multilingual recipe site. Right now, comments are tied to the specific language version of a recipe they were posted on—but we can make them show up across all translated versions of the same recipe with a bit of targeted code.

Core Idea

Most multilingual plugins (like WPML or Polylang) link translated versions of your recipes as a "group" of related posts. The key is to modify WordPress's comment query to pull comments from all posts in that group, not just the current language's post ID.

Solution 1: For WPML Users

WPML provides functions to fetch translated post IDs. We'll use the comments_clauses filter to adjust the comment query's WHERE clause to include all linked recipe versions:

add_filter( 'comments_clauses', 'sync_recipe_comments_wpml', 10, 2 );
function sync_recipe_comments_wpml( $clauses, $wp_query ) {
    // Only run this on single recipe pages, and the main query
    if ( ! is_singular( 'recipe' ) || ! $wp_query->is_main_query() ) {
        return $clauses;
    }

    global $post;
    // Get all translated versions of the current recipe
    $translations = wpml_get_translations( $post->ID );
    $related_post_ids = array( $post->ID );

    if ( ! empty( $translations ) ) {
        foreach ( $translations as $translation ) {
            $related_post_ids[] = $translation->element_id;
        }
    }

    // Update the WHERE clause to pull comments from all related posts
    $clauses['where'] = str_replace(
        'comment_post_ID = ' . $post->ID,
        'comment_post_ID IN (' . implode( ',', array_map( 'intval', $related_post_ids ) ) . ')',
        $clauses['where']
    );

    return $clauses;
}

Solution 2: For Polylang Users

Polylang uses pll_get_post_translations to get linked posts. Here's the equivalent code:

add_filter( 'comments_clauses', 'sync_recipe_comments_polylang', 10, 2 );
function sync_recipe_comments_polylang( $clauses, $wp_query ) {
    if ( ! is_singular( 'recipe' ) || ! $wp_query->is_main_query() ) {
        return $clauses;
    }

    global $post;
    // Fetch all translated recipe IDs
    $translations = pll_get_post_translations( $post->ID );
    $related_post_ids = array_values( $translations );
    // Make sure we include the current post too
    if ( ! in_array( $post->ID, $related_post_ids ) ) {
        $related_post_ids[] = $post->ID;
    }

    // Adjust the comment query to include all related posts
    $clauses['where'] = str_replace(
        'comment_post_ID = ' . $post->ID,
        'comment_post_ID IN (' . implode( ',', array_map( 'intval', $related_post_ids ) ) . ')',
        $clauses['where']
    );

    return $clauses;
}

The above code will already make all comments show up across all language versions, but if you want new comments to be technically linked to your default language's recipe (instead of the specific language version they were posted on), you can add this filter:

add_filter( 'preprocess_comment', 'link_comment_to_main_recipe' );
function link_comment_to_main_recipe( $commentdata ) {
    if ( ! isset( $commentdata['comment_post_ID'] ) ) {
        return $commentdata;
    }

    $post_id = $commentdata['comment_post_ID'];
    $post = get_post( $post_id );

    // Only apply to recipe posts
    if ( $post->post_type !== 'recipe' ) {
        return $commentdata;
    }

    // For WPML: Get the default language version of the recipe
    $main_recipe_id = apply_filters( 'icl_object_id', $post_id, 'recipe', true, null );

    // For Polylang: Uncomment the line below and comment the WPML line above
    // $main_recipe_id = pll_get_post( $post_id, pll_default_language() );

    if ( $main_recipe_id && $main_recipe_id !== $post_id ) {
        $commentdata['comment_post_ID'] = $main_recipe_id;
    }

    return $commentdata;
}

Quick Notes

  • Add this code to your theme's functions.php file, or better yet, a custom plugin (so it doesn't get lost when you update your theme).
  • Double-check that all your recipe translations are properly linked in your multilingual plugin settings—this code relies on those existing relationships.
  • If you're using a different multilingual plugin, the core logic stays the same: fetch all related post IDs for the current recipe, then update the comment query to include those IDs.

内容的提问来源于stack exchange,提问作者Taj Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:32:42