@chriskirknielsen I made an account just to say thank you for a thorough answer, this should be highlighted as the correct answer. I’m surprised this isn’t covered elsewhere, at least to my knowledge.
Many thanks!
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
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?
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?
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);
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.