Syncing custom fields between 2 languages (with WPML)

I’m using WPML (WordPress multilanguage plugin) with custom post and fields (with Advanced Custom Fields plugin) and I have this “issue”:
I’ve a custom post with a custom field (text), I enter text in the field and save. Now I go to the translated post and see that the same custom field is empty. Then the fields ar not synched. Notice that instead the Tag field is well synched between the languages.
Someone can help? Thanks

Related posts

Leave a Reply

3 comments

  1. I don´t think the saved value of a custom field is synced by default. Only the name of the variable etc.

    So if you have a custom field and wan´t it to have the same value on all languages, simply don´t add that custom field to other languages. Just have it on the main language.

    Then in the template you can use this, to allways get the value from main language:

    <?php the_field('fieldname',lang_page_original_id($post->ID));?>
    

    Then add this to functions.php

    function lang_page_original_id($id){
        if(function_exists('icl_object_id')) {
        return icl_object_id($id,'page', false, "MAIN LANGUAGE CODE EX: SV");
        } else {
            return $id;
        }
    }
    
  2. Hi use this in your function.php works 100%:

    function sync_field_meta( $post_id, $post, $update ) {
    
        $post_type = get_post_type($post_id);
        // use this if u have muti custom post type
        $posts_type = array('your_custom_post_type1', 'your_custom_post_type2', 'your_custom_post_type3', 'your_custom_post_type4');
    
        if( ! in_array($post_type, $posts_type)) return;
    
        $en = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'en' );
        $fr = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'fr' );
    
        // your acf key like (field_58136c9dc9963) you can check documention
        $field = get_field('acf_key',$post_id);
    
        if($en){
            update_field('acf_key',$field,$en);
        }
        if($fr){
            update_field('acf_key',$field,$fr);
        }
    
    
    }
    add_action( 'save_post', 'sync_field_meta', 10, 3 );