WPML
apply_filters( 'wpml_copy_post_to_language', int $post_id, string $target_language, bool $mark_as_duplicate )
タイプ
filter
カテゴリー
コンテンツの挿入
WPMLバージョン
4.0.0

説明

このフィルターフックにより、開発者はWPMLを使用して指定した言語に投稿をコピーできます。投稿を翻訳先言語に複製し、複製または翻訳としてマークするオプションを提供します。

引数

$post_id
(int) (必須) コピーする投稿のIDです。
$target_language
(string) (必須) 投稿のコピー先となる翻訳先言語の言語コードです。
$mark_as_duplicate
(bool) (必須) trueに設定すると、複製された投稿は元の投稿の複製としてマークされます。falseの場合、翻訳された投稿として扱われます。

使用例

以下の例では、フィルターを使用してIDが1の投稿をフランス語にコピーし、元の投稿の複製としてマークします。

$duplicate_post_id = apply_filters( 'wpml_copy_post_to_language', 1, 'fr', true );

以下の高度な例では、投稿の元の言語を除き、特定の投稿をWPMLサイトの他のすべての有効な言語へ自動的に複製する方法を示します。

高度な例
function myplugin_duplicate_copy_post_to_secondary_languages( $post_id, $mark_as_duplicate = false ) {
    // Retrieve the post's element type, trid, and existing translations
    $element_type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );
    $trid         = apply_filters( 'wpml_element_trid', null, $post_id, $element_type );
    $translations = (array) apply_filters( 'wpml_get_element_translations', [], $trid, $element_type );

    if ( ! $translations ) {
        // Handle error or invalid values accordingly
        return;
    }

    // Retrieve the active languages
    $wpml_languages = (array) apply_filters('wpml_active_languages', [], 'orderby=id&order=desc');

    foreach ( $wpml_languages as $language ) {
        // Duplicate the post if no translation exists for the given language
        if ( ! isset( $translations[ $language['language_code'] ] ) ) {
            apply_filters(
                    'wpml_copy_post_to_language',
                    $post_id,
                    $language['language_code'],
                    $mark_as_duplicate
            );
        }
    }
}

wpml_set_element_language_details, wpml_make_post_duplicates, wpml_admin_make_post_duplicates, wpml_delete_package, wpml_import_get_trid_from_imported_translations, wpml_register_string, wpml_register_single_string, wpml_show_package_language_ui, wpml_delete_package_action

すべてのコンテンツの挿入フック(10件) →