wp多语言网站polylang开启情况下如何同步不同语言后缀字段的值到默认的值

首先将这个代码粘贴到functions.php

/**
 * Polylang Bulk Meta Sync
 * Source of truth: default key (e.g. product_gallery)
 * Direction: default key → push to all language-specific keys (suffix pattern)
 */

// ─────────────────────────────────────────────────────────────────────────────
// CONFIGURATION — list only your DEFAULT keys here (no language suffixes)
// ─────────────────────────────────────────────────────────────────────────────

function pll_sync_get_default_keys(): array {
    return [
        'product_gallery',
        'product_price',
        'product_mpn',
        'inner_diameter',
        'outer_diameter',
        'weight',
        'abs_included',
        'oem_num',
        'car_brand',
        'car_application',
        'cross_ref',
        'key_features',
        'drawing'
        // Add every default meta key you want synced.
        // The language-specific keys are auto-derived: key_en, key_ru, etc.
    ];
}

// ─────────────────────────────────────────────────────────────────────────────
// CORE ENGINE
// Reads the default key value from the DEFAULT language post,
// then writes key_{lang} on every translated post.
// ─────────────────────────────────────────────────────────────────────────────

/**
 * Sync one post's default meta keys → all its language-specific counterparts.
 *
 * @param int    $source_post_id  The post ID that holds the default key values.
 * @param string $default_lang    The language slug of the source post (e.g. 'en').
 */
function pll_sync_push_default_to_lang_keys( int $source_post_id, string $default_lang ): void {

    if (
        ! function_exists( 'pll_get_post_language' ) ||
        ! function_exists( 'pll_get_post_translations' )
    ) {
        return;
    }

    // All translations of this post: [ 'en' => 101, 'ru' => 102, 'fr' => 103 ]
    $translations = pll_get_post_translations( $source_post_id );

    if ( empty( $translations ) ) {
        return;
    }

    $default_keys = pll_sync_get_default_keys();

    foreach ( $default_keys as $default_key ) {

        // Read value from the SOURCE (default language) post.
        $value = get_post_meta( $source_post_id, $default_key, true );

        // Nothing stored — nothing to push.
        if ( $value === '' || $value === false ) {
            continue;
        }

        // Push to every translated post using the lang-specific key.
        foreach ( $translations as $lang => $translated_post_id ) {

            $lang_key = $default_key . '_' . $lang; // e.g. product_gallery_en

            update_post_meta( (int) $translated_post_id, $lang_key, $value );
        }
    }
}

将下面这个文件放到网站根目录,不是主题目录, 新建sync-bulk-fields.php

<?php
/**
 * File: sync-meta-bulk.php
 *
 * One-time bulk sync:
 *   Default meta key  →  all language-specific keys (suffix pattern)
 *
 * Usage:
 *   wp eval-file sync-meta-bulk.php
 *
 * Optional — limit to specific post types:
 *   wp eval-file sync-meta-bulk.php --post_type=product
 */

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
    die( 'This script must be run via WP-CLI.' );
}

if ( ! function_exists( 'pll_languages_list' ) ) {
    WP_CLI::error( 'Polylang is not active. Aborting.' );
}

// ── Config ────────────────────────────────────────────────────────────────────

// The language whose posts hold the "source of truth" default key values.
$default_lang = 'en'; // ← change if your default language is different

// Post types to process.
$post_type_arg = WP_CLI\Utils\get_flag_value( $assoc_args ?? [], 'post_type', 'product' );
$post_types    = array_map( 'trim', explode( ',', $post_type_arg ) );

// ── Fetch all posts in the default language ───────────────────────────────────

$posts = get_posts([
    'post_type'      => $post_types,
    'posts_per_page' => -1,
    'lang'           => $default_lang,
    'post_status'    => [ 'publish', 'draft', 'private' ],
    'fields'         => 'ids',
]);

$total = count( $posts );

if ( $total === 0 ) {
    WP_CLI::warning( "No posts found for lang='{$default_lang}' and post types: " . implode( ', ', $post_types ) );
    exit;
}

WP_CLI::line( '' );
WP_CLI::line( "Found {$total} posts in '{$default_lang}'. Starting sync..." );
WP_CLI::line( str_repeat( '─', 50 ) );

// ── Progress bar ──────────────────────────────────────────────────────────────

$progress  = WP_CLI\Utils\make_progress_bar( 'Syncing', $total );
$synced    = 0;
$skipped   = 0;
$default_keys = pll_sync_get_default_keys();

foreach ( $posts as $post_id ) {

    $translations = pll_get_post_translations( $post_id );

    // No translations found — skip.
    if ( count( $translations ) <= 1 ) {
        $skipped++;
        $progress->tick();
        continue;
    }

    pll_sync_push_default_to_lang_keys( $post_id, $default_lang );

    $synced++;
    $progress->tick();
}

$progress->finish();

// ── Summary ───────────────────────────────────────────────────────────────────

WP_CLI::line( '' );
WP_CLI::line( '┌─────────────────────────────┐' );
WP_CLI::line( '│        SYNC SUMMARY         │' );
WP_CLI::line( '├─────────────────────────────┤' );
WP_CLI::line( "│  Total posts found : {$total}" );
WP_CLI::line( "│  Posts synced      : {$synced}" );
WP_CLI::line( "│  Posts skipped     : {$skipped} (no translations)" );
WP_CLI::line( "│  Meta keys synced  : " . count( $default_keys ) );
WP_CLI::line( '└─────────────────────────────┘' );
WP_CLI::success( 'Bulk sync complete.' );

这个是要用wp cli来执行的,首先要确保安装wp cli, 如何安装看之前的教程。

然后打开local by flywheel 环境, 打开网站,打开site shell 进入wp cli

然后输入 wp db check 命令查看数据库链接状态,

如果数据库链接有错误,就在wp-config.php里面修改数据库的配置,

// To the actual Local port: define( ‘DB_HOST’, ‘localhost:10013’ );

然后在wp-cli 命令里面执行:

wp eval-file sync-meta-fields.php

sync-meta-fields.php就是你刚才新建的文件,必须存在网站的根目录,

siteshell 的wp-cli 执行目录也是 : C:\Users\Administrator\Local Sites\demo\app\public>

×
wp多语言网站polylang开启情况下如何同步不同语言后缀字段的值到默认的值

wp多语言网站polylang开启情况下如何同步不同语言后缀字段的值到默认的值

首先将这个代码粘贴到functions....

查看更多

AI agent 如何翻译网站(标签+动态内容)

先让AI把网站进行i18n化, …