Skip Navigation

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

Problem:
The client reported that after updating the

post_name

of a translated product, the permalink (URL) does not update to reflect the English version, even after trying to flush rewrite rules.
Solution:
We suggested using a custom function to ensure that when a post is duplicated in WPML, its permalink (slug) updates to match the original post's slug. Here is the function to add to your theme's functions.php file or a custom plugin:

function custom_wpml_set_duplicate_permalink($post_id) {<br />    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {<br />        return;<br />    }<br />    $is_duplicate = get_post_meta($post_id, '_wpml_duplicate', true);<br />    if (!$is_duplicate) {<br />        return;<br />    }<br />    $original_post_id = apply_filters('wpml_object_id', $post_id, get_post_type($post_id), false, wpml_get_default_language());<br />    if (!$original_post_id) {<br />        return;<br />    }<br />    $original_post = get_post($original_post_id);<br />    $original_slug = $original_post->post_name;<br />    if ($original_slug) {<br />        remove_action('save_post', 'custom_wpml_set_duplicate_permalink');<br />        wp_update_post(['ID' => $post_id, 'post_name' => $original_slug]);<br />        add_action('save_post', 'custom_wpml_set_duplicate_permalink');<br />    }<br />}<br />add_action('save_post', 'custom_wpml_set_duplicate_permalink');

We also recommended testing this solution in a sandbox environment and recording the process using a tool like https://www.loom.com/ to better understand the issue.

If this solution does not resolve your issue, or if it seems outdated or irrelevant to your case, please check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. We highly recommend opening a new support ticket for further assistance.

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 3 replies, has 0 voices.

Last updated by Ayelet Oz 3 months, 2 weeks ago.

Assisted by: Bruno Kos.

Author Posts
February 25, 2025 at 9:03 am #16744505

Ayelet Oz

about the second issue - I've already tried that, and now tried again. The post_name updates, but the permalink, the url of the product, stays the same, and not getting the english one. I also tried adding flush_rewrite_rules();, but it didn't help.
any ideas?

February 25, 2025 at 9:20 am #16744564

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

Timezone: Europe/Zagreb (GMT+02:00)

I’ve created a new ticket for this issue:

hidden link

Can you test it using the following code?

function custom_wpml_set_duplicate_permalink($post_id) {
    // Ensure we're not in an autosave routine
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // Check if this is a WPML duplicate
    $is_duplicate = get_post_meta($post_id, '_wpml_duplicate', true);

    if (!$is_duplicate) {
        return;
    }

    // Get the original post ID
    $original_post_id = apply_filters('wpml_object_id', $post_id, get_post_type($post_id), false, wpml_get_default_language());

    if (!$original_post_id) {
        return;
    }

    // Get the original post's slug
    $original_post = get_post($original_post_id);
    $original_slug = $original_post->post_name;

    if ($original_slug) {
        // Update the duplicated post's slug to match the original
        remove_action('save_post', 'custom_wpml_set_duplicate_permalink'); // Prevent infinite loop
        wp_update_post([
            'ID' => $post_id,
            'post_name' => $original_slug
        ]);
        add_action('save_post', 'custom_wpml_set_duplicate_permalink'); // Re-add after update
    }
}
add_action('save_post', 'custom_wpml_set_duplicate_permalink');

Can you try reproducing this issue in the sandbox?

Based on your tests, do you mean that while the `post_name` (slug) updates correctly, the permalink (full URL) does not reflect the English version?

If that’s the case, could you record the process using a tool like **hidden link to show where it fails? This would help me better understand the difference between the slug and the full URL in this scenario.

February 25, 2025 at 9:09 pm #16748111

Ayelet Oz

You asked - Based on your tests, do you mean that while the `post_name` (slug) updates correctly, the permalink (full URL) does not reflect the English version? Yes, exactly.
I couldn't record....

February 26, 2025 at 10:12 am #16750185

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

Timezone: Europe/Zagreb (GMT+02:00)

Is hidden link not working for you for some reason? Maybe you can try hidden link

Or you can send me exact steps on how to reproduce this?

But note that this is already a custom work not covered by support scope, so you should consider https://wpml.org/contractors/.

We can cover only issues related to our WPML, not non-working custom code.

February 27, 2025 at 10:32 am #16755184

Ayelet Oz

I've managed, base on the code above, which I tried also before, I added some more changes to do because I had more conditions that depend on postmeta fields that I don't have in this stage of translation, I used this:
add_action('shutdown', function () use ($id) {.....

Thanks!