This thread is resolved. Here is a description of the problem and solution.
Problem:
The client is trying to fetch a single post by URI and include the post in the original language and its translation to a specified language using GraphQL, similar to how it was done with the Polylang plugin.
Solution:
We recommend using a combination of
wpml_object_id
and
wpml_element_link
to achieve this. Here are the steps:
1. Use the post's URI to find its ID in the original language.
2. With the post ID, apply
wpml_object_id
to get the translated post ID for the specified language code.
3. Use
wpml_element_link
to generate a URL to the translated post.
Here is a simplified code example:
// Assume $uri is the post's URI and $language_code is the language you want the translation for $post_id = url_to_postid($uri); // Convert URI to post ID // Use wpml_object_id to get the translated post ID in the specified language $translated_post_id = apply_filters('wpml_object_id', $post_id, 'post', true, $language_code); if($translated_post_id) { // If a translation exists, fetch the post object $translated_post = get_post($translated_post_id); // Use wpml_element_link to get the correct URL for the translated post $translated_post_url = apply_filters('wpml_element_link', get_permalink($translated_post_id), 'post', $language_code); // Now, $translated_post and $translated_post_url hold the translated post object and URL respectively // You can use these to display the translated post information as needed }
Please note that this is just a suggested approach and it has not been tested. It also goes beyond our support policy, which you can review at https://wpml.org/purchase/support-policy/. If you need further customization, you may consider hiring a contractor from https://wpml.org/contractors/.
Keep in mind that this solution might be irrelevant due to being outdated or not applicable to your 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 the issue persists, 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.
This topic contains 1 reply, has 2 voices.
Last updated by 1 year, 3 months ago.
Assisted by: Bruno Kos.