Skip Navigation

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.

Elementor users - please update WPML to the latest version to maintain compatibility. More details here - https://wpml.org/changelog/2024/12/wpml-4-6-15-critical-update-for-elementor-sites/
Sun Mon Tue Wed Thu Fri Sat
- - 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00
- - - - - - -

Supporter timezone: America/Lima (GMT-05:00)

Tagged: 

This topic contains 1 reply, has 0 voices.

Last updated by Andreas W. 1 day, 3 hours ago.

Assisted by: Andreas W..

Author Posts
December 24, 2024 at 2:32 pm #16544039

ruikaiW

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?

December 24, 2024 at 3:40 pm #16544225

Andreas W.
Supporter

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');

Best regards
Andreas