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 1 month ago.

Assisted by: Itamar.

Author Posts
May 26, 2024 at 6:15 pm #15671488

Itamar
Supporter

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

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

Hi,

I'll need to consult our second-tier supporters about your code. But let's wait for the results of the duplication of the media translation.

Thanks,
Itamar.

May 26, 2024 at 6:22 pm #15671494

aaronM-20

I just checked again and now it's failed:

Duplicate media: please try again (error)

I also see several instances of the following error in the error logs:

26-May-2024 18:21:15 UTC PHP Notice: Function map_meta_cap was called incorrectly. The post type automobile is not registered, so it may not be reliable to check the capability edit_post against a post of that type. Please see <a>Debugging in WordPress for more information. (This message was added in version 4.4.0.) in /home/nginx/domains/gemclassiccarscom.bigscoots-staging.com/public/wp-includes/functions.php on line 6078

Please advise. Thanks.

-- Aaron

May 26, 2024 at 6:48 pm #15671530

Itamar
Supporter

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

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

Hi,

When I go to Media -> Library, I can see 11756 images in English. When I switch to French or any other language, I see the same number - 11756 images. Please see the attached screenshot. It seems the duplication process was completed successfully.

Have you tried to activate the theme and the third-party plugins to check if it solved the issue?

Regards,
Itamar.

2024-05-26_21-42-47.jpg
May 26, 2024 at 7:01 pm #15671581

aaronM-20

I don't think it fixed the issue. This page still shows several cars that don't have featured images:

hidden link

May 26, 2024 at 7:03 pm #15671582

aaronM-20

It seems to be even worse now. This page results in a 404 (clicking from the link in the previous comment):

hidden link

May 26, 2024 at 7:06 pm #15671583

aaronM-20

Resaving the permalink settings page fixed the broken links, but the featured images are definitely still not there.

May 26, 2024 at 7:37 pm #15671597

aaronM-20

I believe I have fixed the problem by running the code indicated below (modified version of what I sent earlier).

Let me know if you think there is anything else I should be concerned about given the error messages we received during the earlier attempt.

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();
}

May 27, 2024 at 7:13 am #15672270

Itamar
Supporter

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

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

Hi,

I'm glad that the problem is solved now!
I can see that images are shown on the links you shared.

About the error messages you received during the earlier attempt, I think there is no place for concern as long as the problem is solved. In any case, with the information I received, there was no indication that the error was related to WPML.

I suggest you continue working, and if you you get some indication that the error is related to WPML, open a new ticket in our support forum.

Regards,
Itamar.

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.