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.

This topic contains 3 replies, has 2 voices.

Last updated by Daniel 10 months, 3 weeks ago.

Assisted by: Nigel.

Author Posts
May 24, 2023 at 8:15 pm #13711187

Daniel

Tell us what you are trying to do?
I'm working on a wordpress script to import posts from an excel file. I dynamically create the english post using 'wpml_set_element_language_details', so fra everything works fine except for the category translation, there's always french category that is inserted.

Here's my code :
$data_post = array(
'post_date' => $article['created_at'],
'post_type' => 'post',
'post_title' => $article['name'] ,
'post_excerpt' => $article['introduction'] ,
'post_content' => $article['text'] ,
'post_status' => $active,
'post_slug' => $article['slug'],
'post_category' => array(117) , // this is the translated category id
);

$post_id = wp_insert_post($data_post);

$attachment_id = get_post_thumbnail_id($translable_id->post_id) ;
if($attachment_id) {
set_post_thumbnail( $post_id, $attachment_id );
update_post_meta( $post_id,'_thumbnail_id', $attachment_id );
}
if( isset($translable_id->post_id) && $translable_id->post_id != 0) {
$wpml_element_type = apply_filters( 'wpml_element_type', 'post' );

$get_language_args = array('element_id' => $translable_id->post_id, 'element_type' => 'post' );
$original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );

$set_language_args = array(
'element_id' => $post_id,
'element_type' => $wpml_element_type,
'trid' => $original_post_language_info->trid,
'language_code' => 'en', //language code of secondary language
'source_language_code' => $original_post_language_info->language_code
);

do_action( 'wpml_set_element_language_details', $set_language_args );
}

Even if I set the translated category id, it's always the french category that is set to the translated post.

I've followed the documentation of 'wpml_set_element_language_details' action and tried different approach, but I got always the same result.

What I'm doing wrong?
Thanks for help

WPML -> version 4.6.3
Wordpress -> version 6.2
default language -> French

Screen Shot 2023-05-24 at 4.04.06 PM.png
May 26, 2023 at 2:35 pm #13723875

Daniel

Hi, I still need assistance on this.
I've changed the default language in WPML > Language > Site Languages, and it works. The a category is the english one.
Does it have a way to change dynamically change the default language instead of changing it in the setting.
Thanks for your assistance

May 26, 2023 at 3:07 pm #13724073

Nigel
Supporter

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

Hi Dan

Your site's default language is French, and you are trying to add English content, and that is indeed where the source of the problem lies.

When you programmatically insert a post, it is characterised as French by WPML (the default language).

I think you just need to change the sequencing a little for this to work.

Insert the post without the category.
Use the wpml_set_element_language_details hook to update the post to specify that it is in fact English.
And then update the post to apply the English category.

You have most of the code in place that you require already, so I assume you can try that.

If you get stuck, let me know.

May 29, 2023 at 4:37 pm #13734339

Daniel

Thanks Nigel ! Your solution resolved my issue.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.