This thread is resolved. Here is a description of the problem and solution.
Problem:
I want to get the permalinks of the translated posts. When is use the WP core function `get_permalink()` with the element_ids of the translated posts, it always returns the permalink of the post in the current language, not the translations.
Solution:
You can use wpml_object_id (https://wpml.org/wpml-hook/wpml_object_id/) to get element’s ID in the current language or in another specified language and then can use either get_permalink to get the permalink of the translated post using the ID obtained from the said hook.
Alternatively you can also temporarily switch to the desired language on the fly and query as necessary and then switch back to the current language.
I have a list of post IDs for translated versions of a post which I got by using the wpml_get_element_translations filter (https://wpml.org/wpml-hook/wpml_get_element_translations/). This gives me an array where the translated post id is in the 'element_id' key.
Now I want to get the permalinks of the translated posts. When is use the WP core function `get_permalink()` with the element_ids of the translated posts, it always returns the permalink of the post in the current language, not the translations.
How can I get the permalinks for the translated posts?
You can use wpml_object_id (https://wpml.org/wpml-hook/wpml_object_id/) to get element’s ID in the current language or in another specified language and then can use either get_permalink to get the permalink of the translated post using the ID obtained from the said hook.
Alternatively you can also temporarily switch to the desired language on the fly and query as necessary and then switch back to the current language, please see following hooks for details:
when I pass the wpml_object_id of the translation to get_permalink, I still allways get the URL fo the page in the current language. From your answer, I gather that this is not expected behaviour. What could cause this?
Your alternaitve approach seems to work – using the wpml_switch_language action before (and after) the get_pemalink call will get me the correct result. Are there any performance implications in this approach? Since I need to get the URLs of many language variants on one page, I would have to switch language quite a lot...
Have you tried passing $ulanguage_code, which if set to a language code, it will return a translation for that language code or the original if the translation is missing? See https://wpml.org/wpml-hook/wpml_object_id/.
Well, it works as above, but I'm still curious: should get_permalink always return the post in the current language, as it does in my case, or should I be able to get a translated post with it?