Skip to content Skip to sidebar

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 1 reply, has 0 voices.

Last updated by Nicolas V. 3 months, 2 weeks ago.

Author Posts
April 7, 2025 at 3:30 pm #16905118

Eirik

Background of the issue:
I'm trying to get the URL to a term in a different language than the request language using the get_term_link() function. Even after changing the current language, the request language is used. The issue seems to be in the home_url_filter method, where the request language is used instead of the current language. Replacing $url_language = $this->get_request_language(); with $url_language = $this->sitepress->get_current_language(); seems to solve the issue when testing. I have tried both to set the current language with do_action('wpml_switch_language', $language_code); and $sitepress->switch_lang($language_code);. The result is the same.

Symptoms:
The get_term_link() function returns an invalid term URL prefixed with /pl/ (request language) resulting in a 404 error when visited, instead of a valid and correct URL prefixed with the current language (/no/) (or without the prefix when it's the the main language).

Questions:
Why does get_term_link() return a URL for the request language instead of the current language?
How can I ensure that get_term_link() returns a URL prefixed with the current language?

April 8, 2025 at 3:15 pm #16910170

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hi,

Welcome to the WPML support forum. Thanks for contacting us.

While waiting for one of my colleagues to help with this ticket, and based on what you've described, allow me to point you to a similar ticket:

https://wpml.org/forums/topic/when-i-use-get_term_linkterm-i-dont-get-the-translated-woocommerce-product-ca/#post-14427771

The solution was to use our hook 'wpml_object_id' to translate the ID before using 'get_term_link()'. Does this solution make sense for your scenario?

All pur hooks are available here: https://wpml.org/wpml-hook/

Looking forward to hearing back from you.

Nico

The topic ‘[Closed] get_term_link() only returns links for the request language, not the current language. The result is…’ is closed to new replies.