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 thread is resolved. Here is a description of the problem and solution.

Problem: The user would like to retrieve categories using REST API Solution: We will need to add a URL(GET) param lang={language_code} to the categories endpoint . Relevant Documentation: https://developer.wordpress.org/rest-api/reference/categories/

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Dan 2 months ago.

Assigned support staff: Jamal.

Author Posts
August 8, 2019 at 8:07 am #4360669

Dan

Hi, Can you please guide how to fetch the translated category. When I pass translated term ID to get_term function it is fetching English version only.

August 8, 2019 at 8:15 am #4360829

Jamal
Supporter

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

Timezone: Africa/Casablanca (GMT+01:00)

Hello Dan,

I am not really sure to understand your question. Could you please elaborate a little more on so I can better understand? Maybe share the code you are using!

In the meantime, I'll suggest a piece of code:

<?php
global $sitepress;
$original_lang = apply_filters( 'wpml_current_language', NULL ); // Save the current language
$target_lang = 'es'; // The language in which you want to get the terms
do_action( 'wpml_switch_language', $target_lang ); // Switch to new language
  
// Query the terms in new language instead of current language
$terms = get_terms( array('taxonomy' => 'category','hide_empty' => false) );
  
// Roll back to current language            
do_action( 'wpml_switch_language', $original_lang ); 
?>

Best regards,
Jamal
WPML Support

August 8, 2019 at 9:42 am #4361521

Dan

Hi, I am using rest API so it is not possible to switch languages because if multiple users will access site same time data error may persist. I am able to get the $target_lang but when I pass it in get_term it is not fetching translated term.

August 8, 2019 at 11:51 am #4362323

Jamal
Supporter

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

Timezone: Africa/Casablanca (GMT+01:00)

Hi,
Do you mean, you are using the custom endpoint for categories https://developer.wordpress.org/rest-api/reference/categories/

If it is the case, you will just need to add ?lang={language_code} to your endpoint.

The code I suggested in my first reply is to be used by your custom controller, or the function to serve your custom REST endpoint. The language switch will only affect the current request, it won't affect other users requests.

Best regards,
Jamal

August 8, 2019 at 12:51 pm #4363071

Dan

My issue is resolved now. Thank you!