<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>多語系 &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/%e5%a4%9a%e8%aa%9e%e7%b3%bb/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Sun, 21 Dec 2025 12:54:02 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://piglife.tw/wp-content/uploads/2017/10/cropped-logo-1-32x32.png</url>
	<title>多語系 &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>使用 Polylang 官方流程批量複製 WordPress 文章翻譯</title>
		<link>https://piglife.tw/technical-notes/polylang-bulk-copy-translations/</link>
					<comments>https://piglife.tw/technical-notes/polylang-bulk-copy-translations/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sat, 20 Dec 2025 22:21:10 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[acf]]></category>
		<category><![CDATA[Polylang]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_insert_post]]></category>
		<category><![CDATA[多語系]]></category>
		<category><![CDATA[翻譯複製]]></category>
		<category><![CDATA[自訂文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/polylang-bulk-copy-translations/</guid>

					<description><![CDATA[介紹如何使用 Polylang 官方 API 批量複製 WordPress 自訂文章類型的內容，建立...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>這段 PHP 程式碼是為了解決 WordPress 使用 Polylang 多語系外掛時，如何批量將特定文章類型的內容從一種語言複製到另一種語言，並建立正確的翻譯關聯。適合需要管理大量多語系內容的網站管理員或開發者，尤其是想自動化翻譯文章複製流程的技術人員。</p>
<h2 class="wp-block-heading">Polylang 官方 API 的使用背景</h2>
<p>Polylang 提供了多個官方函式來管理文章語言與翻譯關聯，如 <code>pll_set_post_language</code> 設定文章語言，<code>pll_save_post_translations</code> 儲存翻譯群組，這是官方推薦的做法，能確保多語系資料完整且一致。</p>
<h2 class="wp-block-heading">後台介面設計與操作流程</h2>
<h3 class="wp-block-heading">後台子選單與頁面</h3>
<p>程式碼透過 <code>add_submenu_page</code> 在自訂文章類型「investor_relations」的管理頁面下新增子選單，提供單筆測試與批量複製兩種操作介面。使用者可選擇目標語言（目前限制為英文與日文），並執行對應動作。</p>
<h3 class="wp-block-heading">權限與安全檢查</h3>
<p>頁面載入時會檢查使用者是否有 <code>manage_options</code> 權限，並確認 Polylang API 函式存在，避免外掛未啟用或權限不足導致錯誤。</p>
<h2 class="wp-block-heading">核心功能：複製文章並建立翻譯關聯</h2>
<h3 class="wp-block-heading">1. 確認來源文章及語言設定</h3>
<p>函式 <code>ir_clone_post_official</code> 會先取得來源文章，並用 <code>pll_set_post_language</code> 強制設定來源文章語言，確保語言標記正確。</p>
<pre><code class="lang-php language-php php">ir_set_lang_official( $source_id, $source_lang );</code></pre>
<h3 class="wp-block-heading">2. 檢查目標語言翻譯是否已存在</h3>
<p>利用 <code>pll_get_post_translations</code> 取得該文章的所有語言版本，若目標語言已存在翻譯文章，則跳過建立，避免重複。</p>
<h3 class="wp-block-heading">3. 建立新文章（翻譯版本）</h3>
<p>使用 WordPress 官方函式 <code>wp_insert_post</code> 複製文章內容與屬性，並設定新文章的語言為目標語言。</p>
<pre><code class="lang-php language-php php">$new_id = wp_insert_post([...]);
ir_set_lang_official( $new_id, $target_lang );</code></pre>
<h3 class="wp-block-heading">4. 儲存翻譯關聯</h3>
<p>將來源文章與新文章的語言關聯資料更新至 Polylang，確保前台語言切換功能正常。</p>
<pre><code class="lang-php language-php php">$translations[ $target_lang ] = $new_id;
pll_save_post_translations( $translations );</code></pre>
<h3 class="wp-block-heading">5. 複製分類法與特色圖片</h3>
<p>排除語言分類法後，複製其他自訂分類法的關聯，並複製特色圖片，保持內容一致性。</p>
<h3 class="wp-block-heading">6. 複製 ACF 自訂欄位資料</h3>
<p>若有安裝 Advanced Custom Fields 外掛，會將所有欄位資料一併複製，包含上傳檔案，避免資料遺失。</p>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<ul>
<li><strong>備份資料庫</strong>：執行批量複製前務必備份，避免誤操作導致資料錯亂。</li>
<li><strong>單筆測試</strong>：先用單筆測試功能確保流程正常，再進行批量操作。</li>
<li><strong>擴充語言</strong>：可依需求新增允許的目標語言陣列。</li>
<li><strong>錯誤處理</strong>：可加強錯誤回報與日誌紀錄，方便除錯與維護。</li>
</ul>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>Polylang API 函式不存在時，需確認外掛是否啟用。</li>
<li>複製文章時，若有其他外掛影響文章資料，可能需要額外處理。</li>
<li>語言分類法不應被複製，以免覆寫 Polylang 的語言設定。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
/**
 * Investor Relations 單語言批量翻譯複製工具（最終版）
 * - 完全走 Polylang 官方流程：
 *   wp_insert_post + pll_set_post_language + pll_save_post_translations
 * - 自動跳過「已經有目標語言翻譯」的文章
 */

define( &#039;IR_SOURCE_LANG&#039;, &#039;zh&#039; );                  // 來源語言：中文
define( &#039;IR_POST_TYPE&#039;, &#039;investor_relations&#039; );    // Post type：investor_relations

/* =========================
 * 共用：設定語言（官方 API）
 * ========================= */
function ir_set_lang_official( $post_id, $lang_code ) {
    if ( function_exists( &#039;pll_set_post_language&#039; ) ) {
        pll_set_post_language( $post_id, $lang_code );
    }
}

/* =========================
 * 後台子選單
 * ========================= */
add_action( &#039;admin_menu&#039;, function () {
    add_submenu_page(
        &#039;edit.php?post_type=&#039; . IR_POST_TYPE,
        &#039;Bulk Copy Translations (Official)&#039;,
        &#039;Bulk Copy Translations (Official)&#039;,
        &#039;manage_options&#039;,
        &#039;ir-bulk-translations-official&#039;,
        &#039;ir_bulk_translations_official_page&#039;
    );
});

/* =========================
 * 後台頁面 + 控制流程
 * ========================= */
function ir_bulk_translations_official_page() {

    // 允許的目標語言（依照 Polylang 的 code）
    $allowed_target_langs = [ &#039;en&#039;, &#039;ja&#039; ];

    if ( ! current_user_can( &#039;manage_options&#039; ) ) {
        wp_die( &#039;沒有權限。&#039; );
    }

    if ( ! function_exists( &#039;pll_set_post_language&#039; ) || ! function_exists( &#039;pll_save_post_translations&#039; ) ) {
        echo &#039;&lt;div class=&quot;notice notice-error&quot;&gt;&lt;p&gt;Polylang API 不存在，請確認外掛是否啟用。&lt;/p&gt;&lt;/div&gt;&#039;;
        return;
    }

    // 目前選擇的目標語言（預設 en）
    $current_target = isset( $_POST[&#039;ir_target_lang&#039;] )
        ? sanitize_text_field( $_POST[&#039;ir_target_lang&#039;] )
        : &#039;en&#039;;

    if ( ! in_array( $current_target, $allowed_target_langs, true ) ) {
        $current_target = &#039;en&#039;;
    }

    echo &#039;
&lt;h1&gt;Investor Relations 翻譯複製工具（官方流程版）&lt;/h1&gt;&#039;;
    echo &#039;
&lt;p&gt;來源語言固定為：&lt;code&gt;&#039; . esc_html( IR_SOURCE_LANG ) . &#039;</code></p>';
    echo '<p style="color:red"><strong>⚠️ 執行前務必先備份資料庫！</strong></p>';

    echo '
.ir-lang-select{margin-left:8px;}';

    /* ------------ 單筆測試表單 ------------- */
    echo '
<p>單筆文章測試</p>';
    echo '';
    wp_nonce_field( 'ir_single_test_official' );
    echo '<label>輸入文章 ID：<input type="number" name="ir_test_id"></label>';

    echo '<span class="ir-lang-select">目標語言：';
    echo '';
    foreach ( $allowed_target_langs as $code ) {
        printf(
            '%s',
            esc_attr( $code ),
            selected( $current_target, $code, false ),
            esc_html( strtoupper( $code ) )
        );
    }
    echo '</span> ';

    submit_button( '測試複製此文章', 'secondary', 'ir_single_test', false );
    echo '';

    /* ------------ 批次表單 ------------- */
    echo '
<p>批量複製所有中文文章</p>';
    echo '';
    wp_nonce_field( 'ir_bulk_official' );

    echo '<span>目標語言：';
    echo '';
    foreach ( $allowed_target_langs as $code ) {
        printf(
            '%s',
            esc_attr( $code ),
            selected( $current_target, $code, false ),
            esc_html( strtoupper( $code ) )
        );
    }
    echo '</span> ';

    submit_button( '執行批次複製（請先單筆測試）', 'primary', 'ir_bulk_run', false );
    echo '';

    /* ------------ 執行動作 ------------- */

    // 單筆
    if ( isset( $_POST['ir_single_test'] ) ) {
        check_admin_referer( 'ir_single_test_official' );
        $post_id = isset( $_POST['ir_test_id'] ) ? intval( $_POST['ir_test_id'] ) : 0;
        $target  = isset( $_POST['ir_target_lang'] ) ? sanitize_text_field( $_POST['ir_target_lang'] ) : 'en';
        ir_run_single_copy_official( $post_id, $target );
    }

    // 批次
    if ( isset( $_POST['ir_bulk_run'] ) ) {
        check_admin_referer( 'ir_bulk_official' );
        $target = isset( $_POST['ir_target_lang'] ) ? sanitize_text_field( $_POST['ir_target_lang'] ) : 'en';
        ir_run_bulk_copy_official( $target );
    }

}</code></pre>
<pre><code class="lang-php language-php php">function ir_run_single_copy_official( $post_id, $target_lang ) {
    echo &#039;
&lt;h3&gt;單筆測試結果&lt;/h3&gt;&#039;;

    if ( ! $post_id || ! get_post( $post_id ) ) {
        echo &#039;&lt;p style=&quot;color:red;&quot;&gt;找不到文章 ID：&#039; . esc_html( $post_id ) . &#039;&lt;/p&gt;&#039;;
        return;
    }

    $report = ir_clone_post_official( $post_id, $target_lang );

    echo &#039;&lt;pre style=&quot;background:white; padding:10px; border:1px solid #ccc;&quot;&gt;&#039;;
    echo esc_html( implode( &quot;\n&quot;, $report ) );
    echo &#039;&lt;/pre&gt;&#039;;
}

/* =========================
 * 批量處理所有中文文章（官方流程）
 * ========================= */
function ir_run_bulk_copy_official( $target_lang ) {
    $posts = get_posts( [
        &#039;post_type&#039;      =&gt; IR_POST_TYPE,
        &#039;posts_per_page&#039; =&gt; -1,
        &#039;post_status&#039;    =&gt; &#039;any&#039;,
        &#039;lang&#039;           =&gt; IR_SOURCE_LANG, // Polylang 的語言 query var
        &#039;fields&#039;         =&gt; &#039;ids&#039;,
    ] );

    echo &#039;
&lt;h2&gt;批次執行結果（目標語言：&#039; . esc_html( strtoupper( $target_lang ) ) . &#039;）&lt;/h2&gt;&#039;;

    if ( empty( $posts ) ) {
        echo &#039;
&lt;p&gt;沒有找到來源語言（&#039; . esc_html( IR_SOURCE_LANG ) . &#039;）的文章。&lt;/p&gt;&#039;;
        return;
    }

    $output = [];

    foreach ( $posts as $post_id ) {
        $report  = ir_clone_post_official( $post_id, $target_lang );
        $output  = array_merge( $output, $report );
    }

    echo &#039;&lt;pre style=&quot;background:white; max-height:450px; overflow:auto; padding:10px; border:1px solid #ccc;&quot;&gt;&#039;;
    echo esc_html( implode( &quot;\n&quot;, $output ) );
    echo &#039;&lt;/pre&gt;&#039;;
}

/* =========================
 * 核心：官方流程複製一篇文章 &rarr; 目標語言翻譯
 * ========================= */
function ir_clone_post_official( $source_id, $target_lang ) {
    $report = [];

    $source_post = get_post( $source_id );
    if ( ! $source_post ) {
        $report[] = &quot;❌ 原文 {$source_id} 不存在&quot;;
        return $report;
    }

    $source_lang = IR_SOURCE_LANG;

    // Step 1) 先確保原文語言正確
    ir_set_lang_official( $source_id, $source_lang );
    $report[] = &quot;Step 1) 設定原文語言：{$source_id} &rarr; {$source_lang}&quot;;

    // 取得現有翻譯 group
    $translations = function_exists( &#039;pll_get_post_translations&#039; )
        ? pll_get_post_translations( $source_id )
        : [];

    if ( empty( $translations[ $source_lang ] ) ) {
        $translations[ $source_lang ] = $source_id;
    }

    // ✅ 若已存在該語言翻譯 &rarr; 自動跳過
    if ( ! empty( $translations[ $target_lang ] ) ) {
        $existing = $translations[ $target_lang ];
        $report[] = &quot;⚠️ 原文 {$source_id} 已有 {$target_lang} 翻譯（ID {$existing}），跳過建立新文章。&quot;;

        // 做個語言 debug
        $lang_debug = [];
        foreach ( $translations as $code =&gt; $pid ) {
            $lang_debug[] = $code . &#039;:&#039; . $pid . &#039;(&#039; . pll_get_post_language( $pid ) . &#039;)&#039;;
        }
        $report[] = &#039;🧪 目前語言狀態：&#039; . implode( &#039;, &#039;, $lang_debug );

        return $report;
    }

    // Step 2) 使用 wp_insert_post 建立新文章（官方建議做法）
    $new_id = wp_insert_post( [
        &#039;post_type&#039;      =&gt; $source_post-&gt;post_type,
        &#039;post_status&#039;    =&gt; $source_post-&gt;post_status,
        &#039;post_title&#039;     =&gt; $source_post-&gt;post_title,
        &#039;post_content&#039;   =&gt; $source_post-&gt;post_content,
        &#039;post_excerpt&#039;   =&gt; $source_post-&gt;post_excerpt,
        &#039;post_author&#039;    =&gt; $source_post-&gt;post_author,
        &#039;post_date&#039;      =&gt; $source_post-&gt;post_date,
        &#039;post_date_gmt&#039;  =&gt; $source_post-&gt;post_date_gmt,
        &#039;menu_order&#039;     =&gt; $source_post-&gt;menu_order,
    ], true );

    if ( is_wp_error( $new_id ) ) {
        $report[] = &quot;❌ wp_insert_post 錯誤：&quot; . $new_id-&gt;get_error_message();
        return $report;
    }
    $report[] = &quot;Step 2) 建立新文章：{$new_id}&quot;;

    // Step 3) 官方建議：先設定新文章語言
    ir_set_lang_official( $new_id, $target_lang );
    $report[] = &quot;Step 3) 設定新文章語言：{$new_id} &rarr; {$target_lang}&quot;;

    // Step 4) 官方建議：建立翻譯關聯（保留既有語言）
    $translations[ $target_lang ] = $new_id;
    pll_save_post_translations( $translations );
    $report[] = &quot;Step 4) 儲存翻譯關聯：&quot; . json_encode( $translations );

    // Step 5) 複製 taxonomy（排除語言 taxonomy，避免覆寫語言）
    $taxes = get_object_taxonomies( IR_POST_TYPE );
    $taxes = array_diff( $taxes, [ &#039;language&#039;, &#039;pll_language&#039; ] );

    foreach ( $taxes as $tax ) {
        $terms = wp_get_object_terms( $source_id, $tax, [ &#039;fields&#039; =&gt; &#039;ids&#039; ] );
        if ( ! is_wp_error( $terms ) ) {
            wp_set_object_terms( $new_id, $terms, $tax, false );
        }
    }

    // Step 6) 複製特色圖片
    $thumb = get_post_thumbnail_id( $source_id );
    if ( $thumb ) {
        set_post_thumbnail( $new_id, $thumb );
    }

    // Step 7) 複製 ACF 欄位（包含上傳檔案）
    if ( function_exists( &#039;get_field_objects&#039; ) &amp;&amp; function_exists( &#039;update_field&#039; ) ) {
        $fields = get_field_objects( $source_id );
        if ( $fields ) {
            foreach ( $fields as $field ) {
                update_field( $field[&#039;key&#039;], $field[&#039;value&#039;], $new_id );
            }
        }
    }

    // 最後：實際語言檢查
    $src_lang_real = pll_get_post_language( $source_id );
    $new_lang_real = pll_get_post_language( $new_id );

    $report[] = &quot;🧪 實際語言檢查：原文 {$source_id} 語言：{$src_lang_real}；新文 {$new_id} 語言：{$new_lang_real}&quot;;
    $report[] = &quot;✅ 完成：原文 {$source_id} &rarr; {$target_lang} 翻譯 {$new_id}&quot;;

    return $report;
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/polylang-bulk-copy-translations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
