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.

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by alexanderA 11 months, 1 week ago.

Assigned support staff: Bruno Kos.

Author Posts
October 15, 2019 at 6:43 am #4754707

alexanderA

Hello. On my site I have used WPML to translate my site from English into Chinese. One English language page has been translated into Chinese. They both have the same slug: construction-koh-samui This is as desired. But when I use the function get_page_by_path() (https://developer.wordpress.org/reference/functions/get_page_by_path/), and pass it the argument of `construction-koh-samui` only the Chinese page is contained in the output. Why is this occurring?

I should perhaps note that the Chinese page is in set to draft.

What I want to do is start with the page slug (not the page title), and return the page ID of that page in the original language, and all translations.

Thanks.

October 15, 2019 at 6:57 am #4754865

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Thank you for contacting WPML support!

Can you tell me if you tried this?
https://wpml.org/forums/topic/using-get_page_by_path-to-retrieve-a-page-in-a-different-language/#post-63003
https://wpml.org/forums/topic/get_page_by_path-with-identical-slugs-returns-wrong-language-in-3-2-7/#post-739951

Regards,
Bruno Kos

October 15, 2019 at 7:42 am #4755139

alexanderA

Hi Bruno,

This link looks promising (https://wpml.org/forums/topic/get_page_by_path-with-identical-slugs-returns-wrong-language-in-3-2-7/#post-739951). But I have a question. Let's say the page exists in its original language--English, and is also translated into both Chinese and French. How can I use the the `wpml_object_id` filter to obtain the ID of both the Chinese and French pages?

Instead of using `wpml_object_id` what if I used the `wpml_element_trid` filter? That would return the TRID of the entire translation group. That could be suitable for my purpose.

Also, in the above link, the WPML support agent says,

>> I tested the code you posted above and it seems that the language that the function shows is always the first link found(the other languages are not displayed).

Can you confirm that this is indeed the behavior that occurs? i.e. does the function always return the *first* post found? I just want to be sure I am correctly understanding the situation.

Thanks.

October 15, 2019 at 10:13 am #4756135

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

I tested that code and if returned the URL in the current language. I also went through various tickets and documentation, it seems like there is no simple filter that would allow you to get both original page ID and the translation ID.

You could try creating 2 functions though, one that would return the original slug, the other one to use language filter, such as:

    $get_translated_id = apply_filters('wpml_object_id', $get_page_info->ID, 'page', 'FALSE', 'zh-hans' );

Or just as you said, possibly the combination of https://wpml.org/wpml-hook/wpml_element_trid/ and https://wpml.org/wpml-hook/wpml_get_element_translations/, but I'm afraid I have no examples at my disposal that would work in the exact way you're after.

You could reach out our Contractors for this though:
https://wpml.org/contractors/

Regards,
Bruno Kos

October 16, 2019 at 5:59 am #4762219

alexanderA

OK. Let me backup and ask a question about TRIDs. Let's say my site's default language is English, and I have used WPML to translate the site into French and Chinese. On my site is a page named 'About.'

* The English version of this page has an ID of 5.
* The French version of this page has an ID of 10.
* The Chinese version of this page has an ID of 15.

All three versions of this page belong to the same 'translation group.' Is that correct? If so, this translation group has one single TRID--is that also correct?

If you can clearly answer those questions, I think I can make a decision as to the best approach to my issue.

Thanks.

October 16, 2019 at 7:23 am #4762797

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Can you tell me if this helps?
https://wpml.org/documentation/support/wpml-tables/#language-information-and-translations

You will find that element_id is the field you will require the most. What you described your last reply seems to be correct.

Regards,
Bruno Kos

October 16, 2019 at 9:15 am #4763995

alexanderA

I had a look at that page, but I still think TRID is what I can use to achieve my goal. To re-state my goal, let's reconsider the site I previously described, with the 'About' page in three different languages. My goal is to execute certain PHP code when any of the three 'About' pages is loaded, e.g.

* English 'About' page
* French 'About' page
* Chinese 'About' page

Furthermore, the only information I have in-hand is the slug of the 'About' page. So to do what I want, my approach is this. Every time a page loads, my PHP code will:

1. Use the slug of the 'About' page to calculate the TRID of the 'About' translation group.

2. Separately, calculate the TRID of the page currently being loaded.

3. Check if the two TRIDs match.

If the two TRIDs match, I can be sure the page being loaded is either the English 'About' page, the French 'About' page, or the Chinese 'About' page, and I can safely execute my remaining PHP code.

Do you see any issues with that?

Thanks.

October 16, 2019 at 10:01 am #4764395

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

That is correct, that sounds like a doable solution. So based on the slug, you can get the page ID. And based on this information, you can find it within icl_translations, in which TRID group.

In case you have the same slugs, you should then check which ID is lower, because it would mean that it was created previously, meaning that it was the original.

Regards,
Bruno Kos

October 16, 2019 at 12:01 pm #4765407

alexanderA

OK thanks, I got it working using TRID. We can consider this resolved.