[Resolved] I need to retrieve posts in different language than current
This thread is resolved. Here is a description of the problem and solution.
Problem: The client needs to query posts of a specific type in a language different from the current display language on a multilingual site. For example, querying posts in Slovak while the site is displayed in English. Solution: We recommend using the
wpml_switch_language
action to switch the language context temporarily for querying posts. Here’s how you can implement it:
do_action('wpml_switch_language', 'sk_SK');
This code switches the current language to Slovak ('sk_SK'), allowing you to query posts in that language. Ensure that the posts/pages you want to query are published and not drafts. After performing the query, you can switch back to the original language if needed. For more details on using this hook, please visit: WPML hooks reference.
Please note that this solution might be outdated or not applicable to your specific case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If this does not resolve your issue, please open a new support ticket.
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.
Hi! For a specific use case, I need to call get_posts() with different language parameter than the site is currently displayed, how to do it?
Imagine having cs_CZ content and I need to load some content of certain post type in sk_SK (for example jobs in Bratislava, that are posted only in Slovak, but I need to show them in Czech listing page).
before your ticket is assigned to one of my colleagues, please allow me to walk you through some initial debugging steps. This will help speed up the support process.
You can use our hooks to do it. I think something to this is what you need :
Hi! I'm able to retrieve the term ("pracoviste") ID in different languages, however, using this query returns nothing since the posts are not in the current laguage. How should I set it up?
$positions_in_sk_cs = get_posts([
'post_type' => 'pozice',
'posts_per_page' => 3, // Doesn't matter in which language
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pracoviste',
'field' => 'term_id',
'terms' => [23,155], // Both SK an CZ place
'include_children' => true
)
)
]);
Languages: English (English )Spanish (Español )German (Deutsch )
Timezone: America/Lima (GMT-05:00)
Hello,
You should be able to add the language as the 4th argument of the 'wpml_object_id' filter hook:
$ulanguage_code
(mixed) (Optional) If missing or NULL, it will use the current language. If set to a language code, it will return a translation for that language code or the original if the translation is missing and $return_original_if_missing is set to TRUE. Default is NULL
Hi, thanks, but I guess we're still not on the same boat:
Basically, I need to query posts of a specific type in a different language than the current. The posts are not translations of anything; they are just posted in one of the languages on the site.
Imagine simply that you're on the EN page and you want to query all posts in the SK language.
I would suggest as a workaround to create the content with the source language EN with SK content and then filter it with a specific taxonomy ID for those contents.