Support

Account

Home Forums Add-ons Gallery Field Gallery field doesn't accept data from other languages

Solving

Gallery field doesn't accept data from other languages

  • Hello,
    I’m trying to migrate/duplicate some posts from english to other languages – the code works fine, until it gets to the gallery fields, which it doesn’t pass to the new post.

    here is my code:

    function updatePostswithImageandFields($posttype){
    
      global  $sitepress;
      $languages = array('en', 'et', 'lv', 'fi', 'pt-pt', 'sl');
    
    $args = array( 
      'post_type' => $posttype,
      'posts_per_page' => -1,
    );
    
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        $wpid = get_the_ID();
        $t_post_id = $sitepress->get_element_trid( $wpid, 'post_'.$posttype );
        $translations = $sitepress->get_element_translations($t_post_id , 'post_'.$posttype, false, true);
        
        $engthumbnailID = get_post_thumbnail_id($wpid);
        $engposts = get_fields($wpid);
    
        foreach($translations as $langkey => $translation){
          $fields = get_fields($translation->element_id); // leftover code when i tried to loop through all the fields
    
            if($posttype == 'regioon')
            {
             
            update_field('region_country', $engposts['region_country'], $translation->element_id); 
              update_field('area', $engposts['area'], $translation->element_id);
              update_field('population', $engposts['population'], $translation->element_id); 
              update_field('region_phone', $engposts['region_phone'], $translation->element_id);
              update_field('e-mail', $engposts['e-mail'], $translation->element_id); 
              update_field('email', $engposts['email'], $translation->element_id); 
              update_field('text_2', $engposts['text_2'], $translation->element_id); 
              update_field('text_3', $engposts['text_3'], $translation->element_id); 
              update_field('gallery', $images, $translation->element_id); 
              }
    
            }
          else if($posttype == 'ettevote')
          {
            update_field('galerii', $engposts['galerii'], $translation->element_id); 
              update_field('video', $engposts['video'], $translation->element_id);
              update_field('country', $engposts['country'], $translation->element_id); 
              update_field('city', $engposts['city'], $translation->element_id);
              update_field('street', $engposts['street'], $translation->element_id); 
              update_field('house', $engposts['house'], $translation->element_id); 
              update_field('aadress', $engposts['aadress'], $translation->element_id); 
              
          }
            
          set_post_thumbnail( $translation->element_id, $engthumbnailID );
          
        }
        
       
      
    endwhile;
    
    }

    The code runs fine, without errors and copies all the fieldinformation and thumbnails.

    But for the gallery fields, it doesnt duplicate them into other languages.
    Has anyone else had a similar problem with gallery fields and multilang plugins, I am using wpml.

  • I posted part of the wrong code.

    this is the correct part of the if check

    if($posttype == 'regioon')
            {
             
            update_field('region_country', $engposts['region_country'], $translation->element_id); 
              update_field('area', $engposts['area'], $translation->element_id);
              update_field('population', $engposts['population'], $translation->element_id); 
              update_field('region_phone', $engposts['region_phone'], $translation->element_id);
              update_field('e-mail', $engposts['e-mail'], $translation->element_id); 
              update_field('email', $engposts['email'], $translation->element_id); 
              update_field('text_2', $engposts['text_2'], $translation->element_id); 
              update_field('text_3', $engposts['text_3'], $translation->element_id); 
              update_field('gallery', $engposts['gallery'], $translation->element_id); 
              }
  • try getting the fields without formatting them

    
    $engposts = get_fields($wpid, false);
    
  • Hello, thanks for the quick reply, but this doesn’t seem to change anything.

    If i var dump the $engposts[‘gallery’] variable, it contains the right arrays, so I dont think the problem is there.

    I’ve also tried adding another gallery field the post type and then duplicating the image there.

    That worked, but only on the same language. So $engposts[‘gallery’] went to $engposts[‘test_gallery’], but not to the other language field.

    are there some filters I have to apply to get another languages gallery fields?

  • The problem is that when using update_field() you need to supply the value in the same format that ACF stores the value in the database. An image field in the database is an array of image IDs and not an array of image data.

    Of the fields in your list, all of the others are storing only “text” values, or at least that’s my guess based on the field names, so they would not be affected.

  • Another issue could be that the images don’t exist. If this is somehow using multisite and the values are being inserted into a different site then the image “POSTS” will not exist on the other site. But to be honest, I don’t know how WPML works.

  • Here are two examples of what getposts returns for two ids – the same post in english and estonian, to which I purposely added the same pictures to two separate gallery fields:

    ENGLISH

    ["gallery"]=>
      array(1) {
        [0]=>
        string(4) "5690"
      }
      ["test_galerii"]=>
      array(1) {
        [0]=>
        string(4) "5694"
      }

    ESTONIAN

    ["gallery"]=>
      array(1) {
        [0]=>
        string(4) "5689"
      }
      ["test_galerii"]=>
      array(1) {
        [0]=>
        string(4) "5695"
      }

    I see that a new ID is produced every time I add an image in another language.
    Is there some other handle i can use to add an image to the gallery, other than the ID, or somehow predict the ID that will come?

  • No, if WMPL required different images for each language then you need to get the image information and insert the images to the media library and then use the new image IDs to update the ACF field. I don’t know the details you’d use for inserting an image for a specific language

    https://codex.wordpress.org/Function_Reference/media_handle_sideload

    Honestly, I don’t know beyond this point. The image IDs used need to match existing images for for the site/language, again, I don’t know how WPML works or what hurdles you need to jump to get this to work.

  • I’d like to add something odd. Apparently after all this time, the images were added anyway, but not the the editor. Meaning they were somehow tied to the post object, but not the editor of it.

    Here is my final code.

    
    global $sitepress;
    $jargs = array( 
        'post_type' => 'ettevote',
        'posts_per_page' => -1,
        'post_status' => 'publish'
      );
      $allposts = array();
      $loop = new WP_Query( $jargs );
      while ( $loop->have_posts() ) : $loop->the_post();
            $wpid = get_the_ID();
            $engposts = get_fields($wpid);
            $orcontent = get_the_content();
      
            $wpml_element_type = apply_filters( 'wpml_element_type', 'ettevote' );
            $t_post_id = $sitepress->get_element_trid( $wpid, 'post_ettevote' );
            $translations = $sitepress->get_element_translations($t_post_id , 'post_ettevote', false, true);
            $original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );  
      
            $aid = get_the_author_meta('ID');
            $region_id = get_user_meta($aid, 'user_region', true); 
            $engthumbnailID = get_post_thumbnail_id($wpid);
    
            $postids = array();
          foreach($translations as $langs){
            array_push($postids, $wpid, $langs->element_id);
            if($langs->element_id){
                $my_post = array(
                    'ID'           => $langs->element_id,
                    'post_author' => $aid,
                );
            
                       set_post_thumbnail( $langs->element_id, $engthumbnailID );
                       $newpost_id = wp_update_post($my_post);
                  
                        $set_language_args = array(
                            'element_id'    => $newpost_id,
                            'element_type'  => $wpml_element_type,
                            'trid'   => $t_post_id,
                            'language_code'   => $langs->language_code,
                            'source_language_code' => $original_post_language_info->language_code
                        );
                        do_action( 'wpml_set_element_language_details', $set_language_args );
              
                        //update ettevote fields
                        update_field('galerii', $engposts['galerii'], $langs->element_id); 
                        update_field('video', $engposts['video'], $langs->element_id);
                        update_field('country', $engposts['country'], $langs->element_id); 
                        update_field('city', $engposts['city'], $langs->element_id);
                        update_field('street', $engposts['street'], $langs->element_id); 
                        update_field('house', $engposts['house'], $langs->element_id); 
                        update_field('aadress', $engposts['aadress'], $langs->element_id);
            }
            else{
                continue;
            }
      
         
      endwhile;
    
    

    Maybe it helps someone in the future

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Gallery field doesn't accept data from other languages’ is closed to new replies.