Please make sure to update to WPML 4.3.4 before reporting any issue

Hi, Amit here, I am the WPML Support Manager, our current ticket queue is high, update your WPML plugins and make sure you meet the minimal requirements for running WPML before reporting an issue please - many tickets are resolved doing that

Please look at our updated list of Known Issues and you can also use our support search to find helpful information and of course review our documentation before opening a ticket.

If you do need to open a ticket please make sure to provide us with all the needed information as described in this page

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 thread is resolved. Here is a description of the problem and solution.

Problem: The translation works perfect, I just have one, for me bug, in the REST API. Assuming I have two languages, german and english, and each language has one page. There is the `?lang=en` parameter which is working fine when I am listing all posts, but when I am now requesting a post directly over the `post_id`, I actually want to get the post in the correct language.

Solution: By default, there's no info about the translation to the REST response. Also, the change that you're looking for (use the ID and the language parameter to get the translation) is not gonna work without some custom coding.

You can check these articles https://wpml.org/wpml-hook/ and https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ in order to create your own routes and get the needed elements.

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by jan-peerS 2 months, 2 weeks ago.

Assigned support staff: Radu.

Author Posts
August 30, 2019 at 2:58 pm #4487991

jan-peerS

Hey. The translation works perfect, I just have one, for me bug, in the REST API. Assuming I have two languages, german and english, and each language has one page (which are translations of each other).

There is the `?lang=en` parameter which is working fine when I am listing all posts:

`hidden link` (Here I get the correct list of the posts, all in english)

However, when I am now requesting a post directly over the `post_id`, I actually want to get the post in the correct language. Let's say the german post has the id "1" whereover the english post has the id "2". Following request will now return the german post, which is not the expected behavior and should return the post with the id "2":

`hidden link`

I debugged into the PHP code and checked if these two posts are actually connected. Following code returns "2" which is the correct english post:

[php]
apply_filters( "wpml_object_id", 1, "post", TRUE, "en" )
[php]

I am now wondering if this implementation is the correct behavior.

August 30, 2019 at 7:51 pm #4489113

Radu
Supporter

Languages: English (English )

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

Hey,

Thank you for reaching out the WPML support!

I looked for a similar case about this and it seems that there was some other case about this right here https://wpml.org/forums/topic/slug-or-id-of-translated-post-via-rest-api/#post-3787483. Can you try to use the code suggested by my colleague in the ticket I attached and tell me how things work this time, please?

Thanks,
Radu

August 30, 2019 at 9:03 pm #4489193

jan-peerS

Thanks for reaching out. Unfortunately this is not working as expected as it just returns both german and english on the `/posts?lang=en`, as the filter is disabled just for those.

No filter is active when targeting one post directly. Here the post response should get switched with an other post. I tried some elegant ways to switch the response, but nothing worked.

The only way what I could think of right now is to re`register_rest_field` each post entry, which is probably not the correct way of doing this I suppose (as it works to overwrite existing rest fields with a new value).

September 3, 2019 at 10:06 am #4503335

Radu
Supporter

Languages: English (English )

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

Hey,

I checked this out with my colleagues and, by default, there's no info about the translation to the REST response. Also, the change that you're looking for (use the ID and the language parameter to get the translation) is not gonna work without some custom coding.

You can check these articles https://wpml.org/wpml-hook/ and https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ in order to create your own routes and get the needed elements. Unfortunately, custom working is outside of our support scope and all I can do is share some documentation links that point you to the right direction.

Thanks,
Radu

September 3, 2019 at 10:48 am #4503639

jan-peerS

Alright perfect. I just wondered if that behavior is the correct one, so I will implement my own routes for that. Thanks for helping 😉