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 4 months ago.
Assisted by: Itamar.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.