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.

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Long Nguyen 1 year, 10 months ago.

Assisted by: Long Nguyen.

Author Posts
September 13, 2023 at 12:48 pm #14390343

Andrea

I'm in the midst of a theme conversion and migration: from an Woocommerce with Polylang Pro to a non-Woocommerce with WPML theme.
After the initial Polylang-to-WPML procedure using your tool (hidden link) and following your how-to guide (https://wpml.org/documentation/related-projects/migrate-polylang-wpml/), I realised that the client had many (if not all) product posts featuring the right/translated title, but the slugs that are the same of the base lang post title. E.g.:

Title it: "Prodotto 1"
Slug it: "prodotto-1"

Title en: "Product 1"
Slug en: "prodotto-1"

Title fr: "Produit 1"
Slug fr: "prodotto-1"

As you can see, the titles are correctly translated but not the slugs. Maybe because the client copied the original post, and forgot to change the slug. I don't know...
So what I did is to run once the following php snippet directly from the functions.php, in order to update all the slugs at once:

$posts = get_posts(array('post_type' => 'product', 'numberposts' => -1));

foreach ($posts as $post) {
$new_slug = sanitize_title($post->post_title);
wp_update_post(
array(
'post_name' => $new_slug
)
);
}
However looks like it doesn't work, meaning that if I dump&die the $posts object, I still get the "post_name" NOT updated. Maybe I've missed something...

Any suggestion are more than welcome

September 15, 2023 at 4:51 am #14400369

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi Andrea,

Thank you for contacting WPML support, I'd be happy to help you with this issue.

You are using a custom code to update the post slug on your site, I would like to inform you that helping you with custom code, is out of the scope of WPML. But I have a suggestion:

According to the documentation https://developer.wordpress.org/reference/functions/wp_update_post/
you should set the post ID in the function wp_update_post() to update the post info for the post. For example:

foreach ($posts as $post) {
	$new_slug = sanitize_title($post->post_title);
	wp_update_post(
		array(
			'ID' => $post->ID, //here
			'post_name' => $new_slug
		)
	);
}

If you are not able to accomplish this, I would recommend you contact one of our certified partners who will be more than happy to help you with this. In this link, you will find a list of our certified partners: https://wpml.org/contractors/

Look forward to your reply.
Thanks