Skip Navigation

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 Bruno Kos 1 year, 3 months ago.

Assisted by: Bruno Kos.

Author Posts
February 23, 2024 at 11:17 pm #15339649

bobbyS-4

I am trying to fetch a single post by URI. The response should include the post in the original language and the translation to the language specified as a language code in the $language variable. Previously, I used the Polylang plugin with the following query:

```
query getPost($uri: String!, $language: String!) {
nodeByUri(uri: $uri) {
...on Post {
...PostFragment
translation(language: $language) {
...PostFragment
}
}
}
}

fragment PostFragment on Post {
id
title
date
modified
content

featuredImage {
node {
mediaItemUrl
}
}
author {
node {
...ContributorFragment
}
}
postAcfs {
editor {
nodes {
...ContributorFragment
}
}
}
categories {
nodes {
uri
name
slug
posts(first: 5) {
nodes {
...PostPreviewFragment
author {
node {
...ContributorFragment
}
}
}
}
}
}
seo {

title
metaDesc
opengraphPublisher
opengraphUrl
opengraphDescription
opengraphImage {
sourceUrl
}
twitterTitle
twitterDescription
twitterImage {
altText
sourceUrl
srcSet
}

}
}

fragment ContributorFragment on User {
slug
uri
name
role
userAcfs {
roleTitle
picture {
node {
mediaItemUrl
}
}
}
}

fragment PostPreviewFragment on Post {
id
title
date
excerpt
uri
slug
featuredImage {
node {
mediaItemUrl
}
}
categories {
nodes {
uri
name
slug
}
}
postAcfs {
editor {
nodes {
slug
uri
}
}
}
}
```

Is there a way I can do this? I couldn't find a similar example in the documentation.

Thank you

February 24, 2024 at 8:32 am #15340035

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

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

Hi,

Thank you for contacting WPML support!

Perhaps you can use a combination of wpml_object_id and wpml_element_link, you can follow these 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.

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

Here's a simplified code example demonstrating how you might use wpml_object_id and wpml_element_link to fetch a post by URI and get its translation:

// 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
}

Note that I haven't tested this, this is just a possible approach and goes outside of the support scope https://wpml.org/purchase/support-policy/, but you can hire our https://wpml.org/contractors/ for additional help.

Regards,
Bruno Kos