Skip Navigation

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.

Also see this code, shared by a user: https://wpml.org/forums/topic/get-permalink-of-translated-post/#post-12834523

Relevant Documentation:
- https://wpml.org/wpml-hook/wpml_current_language/
- https://wpml.org/wpml-hook/wpml_switch_language/
- https://wpml.org/wpml-hook/wpml_element_link/
- https://wpml.org/wpml-hook/wpml_permalink/

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.

Our next available supporter will start replying to tickets in about 7.15 hours from now. Thank you for your understanding.

This topic contains 8 replies, has 2 voices.

Last updated by montagmorgens 1 year, 3 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
January 16, 2023 at 4:41 pm #12828769

montagmorgens

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?

January 17, 2023 at 9:02 am #12831877

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting the support.

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:

- https://wpml.org/wpml-hook/wpml_current_language/
- https://wpml.org/wpml-hook/wpml_switch_language/
- https://wpml.org/wpml-hook/wpml_element_link/
- https://wpml.org/wpml-hook/wpml_permalink/

I hope this 'll help you resolve your issue.

Regards.

January 17, 2023 at 9:38 am #12832165

montagmorgens

Hi Waqas Bin Hasan,

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...

January 17, 2023 at 1:09 pm #12834441

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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/.

Or you can also try https://wpml.org/wpml-hook/wpml_permalink/.

wpml_switch_language has some implications, please see the note https://wpml.org/wpml-hook/wpml_switch_language/ under the description.

Regards.

January 17, 2023 at 1:25 pm #12834523

montagmorgens

Yes, I tried that. I got it working like this now:

$current_lang = apply_filters( 'wpml_current_language', null );
$type         = apply_filters( 'wpml_element_type', get_post_type( $some_post_id ) );
$trid         = apply_filters( 'wpml_element_trid', false, $some_post_id, $type );
$translations = apply_filters( 'wpml_get_element_translations', [], $trid, $type );
$languages    = [];

foreach ( $translations as $lang_code => $translation ) {
	$languages[ $lang_code ] = $translation;

	// Switch to translation lang env, otherwise get_permalink returns the url for current language.
	do_action( 'wpml_switch_language', $lang_code );
	$languages[ $lang_code ]->url = get_permalink( $translation->element_id );
}
// Switch back to current language.
do_action( 'wpml_switch_language', $current_lang );

If i leave out the wpml_switch_language actions, I get the wrong URLs.

January 18, 2023 at 5:24 am #12839189

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for trying that, code looks fine.

Hope your issue is resolved now.

Regards.

January 18, 2023 at 6:58 am #12839613

montagmorgens

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?

January 18, 2023 at 8:49 am #12840761

Waqas Bin Hasan
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

get_permalink will always return in current language, so the way you are doing now (your recent code) is the way to get it work.

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