Support

Account

Home Forums Front-end Issues ACF Frontend Form: add new post multilingual (WPML) Reply To: ACF Frontend Form: add new post multilingual (WPML)

  • Solution:

    [code]
    function duplicate_post_to_secondary_language( $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
    );
    }
    }
    }
    
    add_action('acf/save_post', 'duplicate_post_to_secondary_language', 20);