Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
After initiating the Media Translation in WPML, all historical post translations lost their featured images, and the settings for 'Duplicate uploaded media from original' and 'Duplicate featured image from original' were unchecked.
Solution:
Usually this should be solved with the Media translation options in WPML -> Settings. You can read about it here.
https://wpml.org/documentation/getting-started-guide/media-translation/
However, the user reported that he solved the issue with the following code to bulk update the posts to restore the 'Duplicate uploaded media from original' and 'Duplicate featured image from original' settings:

function duplicate_featured_images_to_translations() {
// Ensure this runs only in the admin area
if (!is_admin()) {
error_log('Not in admin area. Exiting function.');
return;
}

// Get all posts of the custom post type 'automobile'
$args = array(
'post_type' => 'automobile',
'posts_per_page' => -1,
'post_status' => 'publish',
);

$automobiles = new WP_Query($args);

if ($automobiles->have_posts()) {
error_log('Found posts of type automobile.');
while ($automobiles->have_posts()) {
$automobiles->the_post();
$post_id = get_the_ID();
error_log("Processing post ID: $post_id");

// Get all translations for the current post
$trid = apply_filters('wpml_element_trid', null, $post_id, 'post_automobile');
if (!$trid) {
error_log("No TRID found for post ID: $post_id");
continue;
}

$translations = apply_filters('wpml_get_element_translations', null, $trid);
if (!$translations) {
error_log("No translations found for post ID: $post_id");
continue;
}

$update_original_post = false;

foreach ($translations as $lang => $translation) {
$translated_post_id = $translation->element_id;
error_log("Checking translation for language: $lang, post ID: $translated_post_id");

// Check if the translated post has a featured image
if (!has_post_thumbnail($translated_post_id)) {
error_log("No featured image found for translation post ID: $translated_post_id");
$update_original_post = true;
break;
}
}

if ($update_original_post) {
error_log("Updating original post ID: $post_id to duplicate media and featured image.");
// Enable the settings to duplicate media and featured images on the original post
update_post_meta($post_id, '_wpml_media_duplicate', 1);
update_post_meta($post_id, '_wpml_media_featured', 1);

// Update the original post to trigger the duplication
wp_update_post(array(
'ID' => $post_id,
'post_content' => get_post_field('post_content', $post_id) // Just update without changes to trigger duplication
));
} else {
error_log("No update needed for original post ID: $post_id");
}
}
} else {
error_log('No posts of type automobile found.');
}

// Reset Post Data
wp_reset_postdata();
}

This script checks each post and its translations, and if the translation lacks a featured image, it updates the original post to enable media and image duplication.

**** Important! Please make a full site backup (files and DB) before you proceed with those steps****

Please note that this solution might be irrelevant if it's outdated or not applicable to your case. If this doesn't resolve your issue, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the problem persists, please open a new support ticket.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 21 replies, has 2 voices.

Last updated by Itamar 3 weeks ago.

Assisted by: Itamar.

Author Posts
May 26, 2024 at 3:48 pm

aaronM-20

Background of the issue:
I was using the Media Translation feature in WPML on my site hidden link. I initiated the process by clicking on "Start" under "Media Translation".

Symptoms:
After starting the media translation, all historical post translations lost their featured images. The settings for 'Duplicate uploaded media from original' and 'Duplicate featured image from original' were unchecked for all historical posts.

Questions:
Is there a way, in bulk, to update all these posts so they have the 'Duplicate uploaded media from original' and 'Duplicate featured image from original' settings restored?

May 26, 2024 at 4:35 pm
May 26, 2024 at 4:36 pm #15671270

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

I'll be waiting for your reply in case you find any errors or need further help with this issue.

Regards,
Itamar.

May 26, 2024 at 4:43 pm #15671272

aaronM-20

These two just came through, but I'm not sure if they're related. The operation is still running and I'll update you again once it finishes.

26-May-2024 16:41:38 UTC PHP Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/nginx/domains/gemclassiccarscom.bigscoots-staging.com/public/wp-includes/class-wp-user.php on line 216
26-May-2024 16:41:38 UTC PHP Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/nginx/domains/gemclassiccarscom.bigscoots-staging.com/public/wp-includes/class-wp-user.php on line 216

May 26, 2024 at 4:47 pm #15671290

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Indeed, those do not seem to be related to the issue.

I'll wait for your further findings.

May 26, 2024 at 4:53 pm #15671291

aaronM-20

I'm seeing this in the WPML page now:

Translate media: please try again (error)

So, I'm guessing the error message logged is actually related somehow. Please let me know how you advise I proceed. Thanks.

-- Aaron

May 26, 2024 at 4:53 pm #15671292

aaronM-20

Screenshot attached just in case you need it.

Screenshot 2024-05-26 125328.png
May 26, 2024 at 5:10 pm #15671310

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

OK, thanks for trying my suggestion.

Can you please try the same process in a minimum environment?

A minimum environment means switching the theme to a default WordPress theme like the Twenty Twenty-One theme and no active plugins besides WPML and Strings Translation.

Can you try it, please?

I know that if you turn the theme and other plugins off, the 'car' post type will not be there. However, what we are trying to do with this is to duplicate the images in WordPress's media library. Even if you switch the theme and deactivate the third-party plugins, the images should remain there. So please see if, in this case, the process finishes without an error. Then, when you activate everything, the translated cars will show the featured image.

Regards,
Itamar.

May 26, 2024 at 5:21 pm #15671398

aaronM-20

Starting now. So you are aware, it started with far fewer than last time:

loading Translating media. 2511 left

Previously there were over 10k images. I'm not sure if it's picking up where it left of when it had an error last time or if this is because of the plugins being deactivated.

I'll let you know when finished.

May 26, 2024 at 5:22 pm #15671400

aaronM-20

Okay, it received an error almost right away:

loading Translate media: please try again (error)

From the logs:

26-May-2024 17:20:21 UTC Cron reschedule event error for hook: action_scheduler_run_queue, Error code: invalid_schedule, Error message: Event schedule does not exist., Data: {"schedule":"every_minute","args":["WP Cron"],"interval":60}

May 26, 2024 at 5:24 pm #15671401

aaronM-20

Sorry!! Please disregard the previous comment. I still had the old tab opened. The operation is still running.

May 26, 2024 at 5:24 pm #15671402

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

OK, if needed, I'll wait to hear from you.

May 26, 2024 at 5:30 pm #15671431

aaronM-20

Okay, I'm not sure what is going on. Now it has changed to the following:

Duplicating media. 76424 left

That's about 7 times more images than I originally had (and I believe we have 7 languages). Is that expected? I haven't done anything myself, it just jumped way higher.

-- Aaron

May 26, 2024 at 5:36 pm #15671439

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

I'm unsure why it happened.

It might be the actual number of images or because of the theme switch and the deactivation of the plugins.

If that causes a problem, please restore the site from the last backup.

Regards,
Itamar.

May 26, 2024 at 5:43 pm #15671441

aaronM-20

I suppose I'll just let it run, although it's going to take significantly longer to complete. I'm not sure if you'll still be online when it finishes. The good news is that the hard drive space being consumed is still the same, so I don't think any images were actually replicated.

I've been working on a backup plan if this doesn't work. Does the following code look like it might be feasible as a solution? I used some help from the web and AI, so there certainly could be issues:

function duplicate_featured_images_to_translations() {
// Ensure this runs only in the admin area
if (!is_admin()) {
return;
}

// Get all posts of the custom post type 'automobile'
$args = array(
'post_type' => 'automobile',
'posts_per_page' => -1,
'post_status' => 'publish',
);

$automobiles = new WP_Query($args);

if ($automobiles->have_posts()) {
while ($automobiles->have_posts()) {
$automobiles->the_post();
$post_id = get_the_ID();

// Get all translations for the current post
$translations = apply_filters('wpml_get_element_translations', null, apply_filters('wpml_element_trid', null, $post_id, 'post_automobile'));

if ($translations) {
foreach ($translations as $lang => $translation) {
$translated_post_id = $translation->element_id;

// Check if the translated post has a featured image
if (!has_post_thumbnail($translated_post_id)) {
// Enable the settings to duplicate media and featured images
update_post_meta($translated_post_id, '_icl_duplicate_media', 1);
update_post_meta($translated_post_id, '_icl_duplicate_featured_image', 1);

// Get the featured image of the original post
$featured_image_id = get_post_thumbnail_id($post_id);

if ($featured_image_id) {
// Set the featured image for the translated post
set_post_thumbnail($translated_post_id, $featured_image_id);
}

// Update the post to trigger the duplication
wp_update_post(array(
'ID' => $translated_post_id,
'post_content' => get_post_field('post_content', $translated_post_id) // Just update without changes to trigger duplication
));
}
}
}
}
}

// Reset Post Data
wp_reset_postdata();
}

aaronM-20 confirmed that the issue was resolved on 2024-05-27 11:59:21.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.