How to get posts in different language from WPML Plugin [closed]
IT Nursery
May 12, 2022
0
i’m using Thermal-Api plugin in order to define a REST WS to get posts in JSON format.
My Client uses WPML to translate posts and i need to pass to WP_Query the language set in front-end side.
Is there a particular parameter to pass to WP_Query to select only the post in that language or is there a way to change dynamically “ICL_LANGUAGE_CODE” in order to retrieve what i need??
Thanks!!
1 Answer 1
Assuming $new_lang holds the desired two letter language code (e.g. ‘fr’), you can do this:
global $sitepress;
$current_lang = $sitepress->get_current_language(); //save current language
$sitepress->switch_lang($new_lang);
//...run query here; if you use WP_Query or get_posts make sure you set suppress_filters=0 ...
$sitepress->switch_lang($current_lang); //restore previous language
For more info, check http://wpml.org/documentation/support/achieving-wpml-compatibility-for-your-themes-and-plugins/debugging-theme-compatibility/