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.

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: 

This topic contains 4 replies, has 1 voice.

Last updated by Dražen 9 hours, 52 minutes ago.

Assisted by: Dražen.

Author Posts
February 21, 2025 at 1:06 pm #16733687

dmytroZ-5

Background of the issue:
I am trying to attach translated taxonomies to posts in different languages using WPML. I used the following code:

 $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:
When a taxonomy is attached to a post in a non-default language, the taxonomy of the default language is attached instead. This happens for both custom and default taxonomies and posts. All IDs are returned correctly, but the wrong category is attached as a result. The issue seems to occur in the wp_set_post_terms() call.

Questions:
Why is the wrong category being attached to translated posts?
How can I ensure the translated category is attached instead of the main language category?

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.
Instead, use apply_filters wpml_element_trid

2)
wpml_set_element_language should not be used.
wpml_set_element_language_details is correct

3)
apply_filters( 'wpml_object_id', $term->term_id, 'doc_category', false, $lang ) might return NULL if the translation does not exist.
Ensure that wpml_object_id is returning valid term IDs.

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,
Drazen

February 26, 2025 at 9:48 am #16750031

dmytroZ-5

Thanks for the advice, but I gave this code as an example.
My code works as expected.

The problem is in wp_set_post_terms(). It assigns the wrong term.
For the sake of experimentation, I did this separately from my code.
I found the necessary ids in the database and inserted them directly into the code.
In result wp_set_post_terms() function assigned a term of the default language.
I did this not only for "doc_category" but also for regular posts and the default WordPress "category."
The result is the same, the default language "category" is assigned.
E.g.

{
    //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,
Drazen