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 9 replies, has 2 voices.

Last updated by Radu 4 years, 5 months ago.

Assisted by: Radu.

Author Posts
June 26, 2020 at 10:42 am #6460827

j.p.d

Hi there,

I'm, trying to get translated terms with get_the_terms();

I do not get any results on the front-end, though I have translated the terms in WPML -> Taxonomy Translation. The result in the second language is just blank. I tried several (5?!) "solutions" from the forum but I can't get it to work.

This is the code currently have hidden link
It works with the default language.

Here you can see I have translated the terms: hidden link

Can you assist me on this one?

Thanks in advance.

June 29, 2020 at 10:03 am #6474491

Radu

Hey there,

Thank you for reaching out to the WPML support!

As I can see, you're using the ID as the first parameter for the get_the_terms function. In this case, I think that you should use this WPML hook:
https://wpml.org/wpml-hook/wpml_object_id/

and get the ID of the translated post. Once you did this, try to get the terms using the new ID. Can you give this a try and tell me how it goes, please?

Thanks,
Radu

June 29, 2020 at 2:04 pm #6477601

j.p.d

Hi Radu,

Thank you for your reply.

Honestly, I have no idea how to implement that example / hook. Tried a couple of things without a result..
Is it possible to assist me a bit on this one?

Really appreciated.

Thanks in advance.
Justin

June 30, 2020 at 7:14 am #6482389

Radu

Hey Justin,

You can try to replace this line:

$terms = get_the_terms( $post->ID, 'ta_type' );

with this code:

$current_ID = apply_filters( 'wpml_object_id', $post->ID, 'ta_type' );
$terms = get_the_terms( $current_ID, 'ta_type' );

Also, you need to check what's the value of the $post->ID variable too, and see what's the value that's returning both in the original and translated page. This way you might be able to identify the ID that's used and see why the returned terms are not correct.

Thanks,
Radu

June 30, 2020 at 9:37 am #6484277

j.p.d

Hi Radu,

Thanks again for your response.

Though I think something is not working correctly since the posts aren't translated, only the terms. Those posts cannot be translated since they are automatically generated. I have translated the terms, not the posts. The settings of the posts are set to "Not translatable".

Unfortunately your code doesn't change anything. Any idea?

Thanks in advance.
Justin

July 1, 2020 at 10:06 am #6493561

Radu

Hey Justin,

I'm sorry but I don't understand what do you mean by automatically generated posts.
So you don't translate the posts, but use some terms on them, translated these terms, and then want to display on the same post both original and translated posts?

I just want to understand what you're trying to do so I can tell you for sure whether this can be done or not.

Thanks,
Radu

July 1, 2020 at 2:30 pm #6496659

j.p.d

Hi Radu,

I'm sorry I wasn't too clear about this matter.

It's like this:

  • the post type itself is set to be "Not translatable" in WPMU settings: hidden link;
  • the terms attached to the post are translatable: hidden link;
  • I want to show the 'term' in the language which is currently active

The reason the posts are set to "Not translatable" is because these posts are automatically generated when a user did a transaction on the website. That post doesn't need to be translated, but the 'term' is like "Bought" or "Sold" or "Refund". Which needs to be translated in different languages.

Hope I gave you enough information.

Kind regards,
Justin

July 2, 2020 at 11:20 am #6503351

Radu

Hey Justin,

As long as the transaction posts are not translated, this means that all of them will be assigned to the original language. In this case, the terms won't be translated.

In order to have translated terms, you need to have a translated post. This way you'll know which terms should be displayed (the one from the original language or the translated ones).

Also, can you tell me how these transactions are created? Are these a part of a plugin? Maybe I can think of a workaround to apply in this case.

Thanks,
Radu

July 6, 2020 at 9:10 am #6528485

j.p.d

Hi Radu,

Your time is appreciated!

Isn't is possible to switch to a language (which is active?) to get a translated term? On the forums I saw possibilities to switch to a specific / active language to get something in a language.

If that is possible, isn't it possible to get terms of a post in a specific or active language?

The transactions are generated by custom code, created by our backend developer. But the post is created only in the default language (English). But the terms are translated.

Thanks in advance.
Justin

July 7, 2020 at 10:26 am #6537827

Radu

Hey Justin,

For now, the only hook that you can use for changing the language is this one:
https://wpml.org/wpml-hook/wpml_switch_language/

However, as you can see, you need to use it only with the pre_get_posts WordPress hook.
Since the code that you're using is a custom one, I'm afraid that helping with this is outside of our support scope. While we can help with small CSS changes and advice about using WPML hooks here and there, what you're looking for would require some more work and I'm afraid I cannot help with that.

Thanks,
Radu