Skip Navigation

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 6 replies, has 3 voices.

Last updated by karolinaV 1 year, 9 months ago.

Assisted by: Andreas W..

Author Posts
April 28, 2023 at 12:48 pm #13557599

karolinaV

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).

Thanks in advance.

April 28, 2023 at 12:54 pm #13557619

Andreas

Hi,

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 :

https://wpml.org/wpml-hook/wpml_object_id/

But you can check all of our hooks in order to find the correct one based on your needs :

https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

Regards,
Andreas

May 3, 2023 at 3:14 pm #13580027

karolinaV

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
            )
        )
    ]);
May 3, 2023 at 10:10 pm #13582187

Andreas W.
Supporter

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

Details:
https://wpml.org/wpml-hook/wpml_object_id/

Please take note, that I did not test this yet, as WPML by default is designed to return the content based on the current site language.

Another way might be to get the trid (translation ID) of the group:
https://wpml.org/wpml-hook/wpml_element_trid/

Save this trid to a variable and use it inside this hook:
https://wpml.org/wpml-hook/wpml_get_element_translations/

This would return an array with all the translations:

Example:

$translations = apply_filters( 'wpml_get_element_translations', NULL, 2, 'post_page' );
var_dump($translations);
/**
 * The result looks like the following 
 * 
array(2) {
  ["en"]=>
  object(stdClass)#1857 (8) {
    ["translation_id"]=>
    string(1) "1"
    ["language_code"]=>
    string(2) "en"
    ["element_id"]=>
    string(1) "2"
    ["source_language_code"]=>
    NULL
    ["element_type"]=>
    string(9) "post_page"
    ["original"]=>
    string(1) "1"
    ["post_title"]=>
    string(4) "Home"
    ["post_status"]=>
    string(7) "publish"
  }
  ["es"]=>
  object(stdClass)#1856 (8) {
    ["translation_id"]=>
    string(3) "141"
    ["language_code"]=>
    string(2) "es"
    ["element_id"]=>
    string(3) "187"
    ["source_language_code"]=>
    string(2) "en"
    ["element_type"]=>
    string(9) "post_page"
    ["original"]=>
    string(1) "0"
    ["post_title"]=>
    string(6) "Inicio"
    ["post_status"]=>
    string(7) "publish"
  }
}
 */

From there you should be able to call anything you need from this array in your theme's template.

Complete Guide:
https://wpml.org/faq/how-to-get-other-languages-of-current-post/

Best regards
Andreas

May 4, 2023 at 11:35 am #13585829

karolinaV

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.

May 4, 2023 at 7:04 pm #13589747

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

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

Hello,

I understand, and this is in fact a uncommon approach.

I am sorry, but as far as I see we do have not any hook for that available.

We can only use the hooks available here:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

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.

Best regards
Andreas

May 5, 2023 at 9:34 am #13592707

karolinaV

Jan Stetina from OTGS pointed this hook to me:

do_action( 'wpml_switch_language', string $language_code )

and that works fine https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-662213

However, the post/page where you want to use it must be published, not a draft.