[Resolved] how to sync publish status for product ?
This thread is resolved. Here is a description of the problem and solution.
Problem: If you're experiencing issues where changing a product's status to 'draft' in the default language (Italian) does not automatically sync the status to additional languages (English/Chinese) on your multilingual site, we have a solution for you. Solution: We recommend adding a custom code snippet to your theme's functions.php file to synchronize the publish status across all languages. Here's how you can do it:
function sync_publish_status_across_languages($post_id) {
// Check if this is a valid post type (product) and that the post is not being autosaved
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ('product' != get_post_type($post_id)) return; // Check for WooCommerce product post type
// Get the current post status
$post_status = get_post_status($post_id);
// Get the default language
$default_language = apply_filters('wpml_default_language', null);
// Get all translations of the post in different languages
$translations = apply_filters('wpml_get_element_translations', null, $post_id, 'product'); // Updated for products
foreach ($translations as $lang => $translation) {
// Skip the default language post (we don't want to update it)
if ($lang === $default_language) continue;
// Get the translation post ID
$translation_post_id = $translation->element_id;
// Update the publish status of the translated post
wp_update_post(array(
'ID' => $translation_post_id,
'post_status' => $post_status
));
}
}
add_action('save_post', 'sync_publish_status_across_languages');
Please note that this solution might be outdated or not applicable to your specific case. If this does not 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 you still need assistance, please open a new support ticket at WPML support forum.
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.
Background of the issue:
I have 3 languages on my site, with Italian as the default and English/Chinese as additional languages. When I change a product's status from 'published' to 'draft' in Italian, the Italian product disappears, but the English and Chinese versions remain published. I want to automate the status change for all languages.
Symptoms:
When changing a product's status to 'draft' in the default language (Italian), the status does not automatically sync to additional languages (English/Chinese).
Questions:
Is there a way to automatically sync the publish status across all languages when changing it in the default language?
Languages: English (English )Spanish (Español )German (Deutsch )
Timezone: America/Lima (GMT-05:00)
Hello,
WPML does handle the status of each content in each language independently.
You could try using a code snippet, like the following example. If you save this snippets inside the function.php file of your theme and then save a original post, page or product, it should sync the status into all active languages.
function sync_publish_status_across_languages($post_id) {
// Check if this is a valid post type and that the post is not being autosaved
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ('post' != get_post_type($post_id)) return;
// Get the current post status
$post_status = get_post_status($post_id);
// Get the default language
$default_language = apply_filters( 'wpml_default_language', null );
// Get all translations of the post in different languages
$translations = apply_filters( 'wpml_get_element_translations', null, $post_id, 'post' );
foreach ($translations as $lang => $translation) {
// Skip the default language post (we don't want to update it)
if ($lang === $default_language) continue;
// Get the translation post ID
$translation_post_id = $translation->element_id;
// Update the publish status of the translated post
wp_update_post(array(
'ID' => $translation_post_id,
'post_status' => $post_status
));
}
}
add_action('save_post', 'sync_publish_status_across_languages');
thanks, I workaround'ed it by search SKU through dtabase , since all 3 languages's product shows same SKU but with different post id , so I extract the post ID by returning SKU-matched query , then manually update the postmeta one by one
Languages: English (English )Spanish (Español )German (Deutsch )
Timezone: America/Lima (GMT-05:00)
The code I earlier provided to you would only work for WordPress Posts or Pages.
If you are working with WooCommerce Products you will need to adapt the code:
function sync_publish_status_across_languages($post_id) {
// Check if this is a valid post type (product) and that the post is not being autosaved
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ('product' != get_post_type($post_id)) return; // Check for WooCommerce product post type
// Get the current post status
$post_status = get_post_status($post_id);
// Get the default language
$default_language = apply_filters('wpml_default_language', null);
// Get all translations of the post in different languages
$translations = apply_filters('wpml_get_element_translations', null, $post_id, 'product'); // Updated for products
foreach ($translations as $lang => $translation) {
// Skip the default language post (we don't want to update it)
if ($lang === $default_language) continue;
// Get the translation post ID
$translation_post_id = $translation->element_id;
// Update the publish status of the translated post
wp_update_post(array(
'ID' => $translation_post_id,
'post_status' => $post_status
));
}
}
add_action('save_post', 'sync_publish_status_across_languages');