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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - |
- | 12:00 – 16:00 | 12:00 – 16:00 | 12:00 – 16:00 | 12:00 – 16:00 | 12:00 – 16:00 | - |
Supporter timezone: Europe/Zagreb (GMT+01:00)
Tagged: Custom Work
This topic contains 4 replies, has 1 voice.
Last updated by Dražen 13 hours, 26 minutes ago.
Assisted by: Dražen.
Author | Posts |
---|---|
February 21, 2025 at 1:06 pm #16733687 | |
dmytroZ-5 |
Background of the issue: $post_id = 1252; //main post $trid = wpml_get_content_trid( 'post_docs', $post_id ); $default_post_categories = wp_get_post_terms( $post_id, 'doc_category' ); $languages = ['de', 'ru', 'uk']; foreach ( $languages as $lang ) { $translated_post_id = wp_insert_post( [ 'post_title' => 'title', 'post_content' => 'content', 'post_status' => 'publish', 'post_type' => 'docs', ] ); if ( ! is_wp_error( $translated_post_id ) ) { do_action( 'wpml_set_element_language', 'post_docs', $translated_post_id, $lang ); do_action( 'wpml_set_element_language_details', [ 'element_id' => $translated_post_id, 'element_type' => 'post_docs', 'trid' => $trid, 'language_code' => $lang ] ); if ( ! empty( $default_post_categories ) ) { foreach ( $default_post_categories as $term ) { $object_id = apply_filters( 'wpml_object_id', $term->term_id, 'doc_category', false, $lang ); if ( $object_id ) { wp_set_post_terms( $translated_post_id, [ $object_id ], 'doc_category' ); } } } } } Link to a page where the issue can be seen: Symptoms: Questions: |
February 24, 2025 at 2:13 pm #16741507 | |
dmytroZ-5 |
{ /** main post id*/ $post_id; $trid = wpml_get_content_trid( 'post_docs', $post_id ); $default_post_categories = wp_get_post_terms( $post_id, 'doc_category' ); $languages = [ 'de', 'ru', 'uk' ]; foreach ( $languages as $lang ) { $translated_post_id = wp_insert_post( [ 'post_title' => 'title', 'post_content' => 'content', 'post_status' => 'publish', 'post_type' => 'docs', ] ); if ( ! is_wp_error( $translated_post_id ) ) { do_action( 'wpml_set_element_language', 'post_docs', $translated_post_id, $lang ); do_action( 'wpml_set_element_language_details', [ 'element_id' => $translated_post_id, 'element_type' => 'post_docs', 'trid' => $trid, 'language_code' => $lang ] ); if ( ! empty( $default_post_categories ) ) { foreach ( $default_post_categories as $term ) { $object_id = apply_filters( 'wpml_object_id', $term->term_id, 'doc_category', false, $lang ); if ( $object_id ) { wp_set_post_terms( $translated_post_id, [ $object_id ], 'doc_category' ); } } } } } } |
February 26, 2025 at 8:42 am #16749661 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, thanks for contacting us. Please note as per our policy we can not debug or provide custom code solutions, for that I advise using WPML contractor's service: https://wpml.org/contractors/ I can advise what would be the correct way to do this. Looking at your code, your approach is mostly correct, but there are a few potential issues that might be causing it not to work as expected. 1) The function wpml_get_content_trid() is not a native WPML function. 2) 3) If you need further help, as said best would be to hire WPML contractors. If you have doubts or questions about our hooks, let me know and I would be glad to help. Regards, |
February 26, 2025 at 9:48 am #16750031 | |
dmytroZ-5 |
Thanks for the advice, but I gave this code as an example. The problem is in wp_set_post_terms(). It assigns the wrong term. { //default lang in system is 'en' $default_lang_term_id = 48; //ID of the category of default language (language 'en') $default_lang_post_id = 4418; //Post ID of default language (post type 'post', language 'en') $translated_term_id = 85; //ID of the of translated category (language 'de') $translated_post_id = 4568; //Post ID of translated post (post type 'post', language 'de') wp_set_post_terms( $translated_post_id, [ $translated_term_id ], 'category' ); //in result: post with ID: 4568 has attached category with ID: 48 (expected ID: 85) } |
February 26, 2025 at 9:58 am #16750061 | |
Dražen Supporter
Languages: English (English ) Timezone: Europe/Zagreb (GMT+01:00) |
Hello, thanks for getting back. We can take a look if you think this is a WPML bug and advise on the correct approach, I have created a test site. Please try to show us an issue with a simple example code for WP posts and share steps on what you did. Please do not add any additional extra code, plugins, or themes. - hidden link Regards, |