Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are developing a bilingual site and have a custom post type called 'portfolio'. While the taxonomy names display correctly in French, they appear in French instead of English on the English page, despite having translations set in WPML.
Solution:
First, ensure that you have translated the taxonomy slug or label via WPML > String Translation. Use the filter hook

wpml_translate_single_string

to retrieve the translated string. Here's how you can implement it:


$taxonomy_objects = get_object_taxonomies( 'post', 'objects' );
foreach ($taxonomy_objects as $taxonomy) {
    $tax_slug = apply_filters( 'wpml_translate_single_string', $taxonomy->name, 'WordPress', 'URL '. $taxonomy->name .' tax slug' );
 
    echo $tax_slug;
    echo "<br />";
}

For more details, follow this documentation: https://wpml.org/wpml-hook/wpml_translate_single_string
If you prefer using SQL queries, you can fetch the translated name directly from the database using the following query:


SELECT st.value AS translated_text
FROM wp_icl_strings s
JOIN wp_icl_string_translations st ON s.id = st.string_id
WHERE s.context = 'WordPress' -- string domain
  AND s.value = 'category'
  AND st.language = 'fr'
  AND st.status = 10; -- completed status

Please test this on your staging site before applying it to your production site.

If this solution does not resolve your issue or seems outdated, we recommend opening a new support ticket. Also, check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum: WPML Support Forum.

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.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Long Nguyen 1 week, 2 days ago.

Assisted by: Long Nguyen.

Author Posts
October 26, 2024 at 11:36 am #16333187

Ankit Kumar

Background of the issue:
I am developing a site in two languages, French (FR) and English (EN), with French as the default language. I have a custom post type (CPT) called 'portfolio'. In the default language, everything works fine using the code: $all_taxonomies = get_object_taxonomies('portfolio', 'objects'); foreach ($taxonomies as $taxonomy) { echo $taxonomy->name }.

Symptoms:
On the English page, I still get the taxonomy name in French instead of English.

Questions:
How can I get the translated taxonomy name instead of the slug?
Is there a way to retrieve the translated taxonomy name using WPML?

October 28, 2024 at 1:48 am #16335664

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi Ankit,

Thank you for contacting WPML support, I’m happy to help you with this issue.

From WPML > String Translation, you can translate the taxonomy slug or label. Then use the filter hook "wpml_translate_single_string" to show the translation string. Here is an example:

$taxonomy_objects = get_object_taxonomies( 'post', 'objects' );

foreach ($taxonomy_objects as $taxonomy) {
	$tax_slug = apply_filters( 'wpml_translate_single_string', $taxonomy->name, 'WordPress', 'URL '. $taxonomy->name .' tax slug' );

	echo $tax_slug;
	echo "<br>";
}

Please check the attached screenshot and follow the documentation
https://wpml.org/wpml-hook/wpml_translate_single_string/

Note: I would like to inform you that helping you with custom code, is out of the scope of WPML. If you are not able to accomplish this, I would recommend you contact one of our certified partners who will be more than happy to help you with this. In this link, you will find a list of our certified partners: https://wpml.org/contractors/

Thanks.

test editor post – WPML 2024-10-28 08-34-09.png
test editor post – WPML 2024-10-28 08-34-29.png
String Translation ‹ WPML — WordPress 2024-10-28 08-27-48.png
October 28, 2024 at 12:57 pm #16337780

Ankit Kumar

"Well, I am already doing it that way; check sc1.png. However, the issue is that my client added a new taxonomy and provided a translated name under WPML → Taxonomy Translation (e.g., check sc2.png).

Now, the question is: if I've already added the translated name in the backend, why do I still need to add an entry in WPML → String Translation?"

Isn't there a database query I can use to fetch the translated name directly since it's already saved?

sc2.png
sc1.png
October 29, 2024 at 1:46 am #16339991

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

If you add the translation in WPML > Taxonomy Translation, the translation is also available in WPML > String Translation. You can translate the taxonomy slug in both places.
Go to WPML > String Translation page to get the correct domain and name of the string to use the filter hook "wpml_translate_single_string".

Regarding the database query, there isn't a query in the documentation to get the translation string directly from the database. However, if you want to use SQL query, please take a look at 2 tables in the database:
- wp_icl_strings
- wp_icl_string_translations

Following my testing case above, here is an example of the SQL query to get the translation string.

SELECT st.value AS translated_text
FROM wp_icl_strings s
JOIN wp_icl_string_translations st ON s.id = st.string_id
WHERE s.context = 'WordPress' -- string domain
  AND s.value = 'category'
  AND st.language = 'fr'
  AND st.status = 10; -- completed status

Note: I cannot guarantee that it can work on your site or any side effects. Please test this on your staging site before applying it to your production site.

Thanks.

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