Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is developing a bilingual site (EN + FR) using WordPress and WPML plugins and is trying to fetch data for a custom post type called 'news-post' using the REST API. However, they are unable to retrieve both English and French data for the 'news-post' custom post type.
Solution:
If you're experiencing this issue, we recommend trying the following steps:
1. To access the French data for your custom post type, append '/fr' to your REST API URL like this:

https://***.ca/fr/wp-json/wp/v2/news-post/

.
2. If you need to retrieve the translated ID in the REST API response, you can use the wpml_object_id hook. This hook allows you to retrieve the corresponding translation of a post, page, or custom post type in the desired language. You can then modify the API output using a filter like rest_prepare_post to include the translated version’s ID based on the specified language.
For more detailed information on working with WPML tables and hooks, you can check the following documentation:

Please note that this solution might be irrelevant if it’s 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 3 replies, has 0 voices.

Last updated by Marcel 1 month, 2 weeks ago.

Assisted by: Marcel.

Author Posts
February 10, 2025 at 10:37 pm #16689656

paulM-12

Background of the issue:
I am developing a bilingual site (EN + FR) using WordPress and have installed the WPML plugins. I am trying to fetch data from this WordPress site to another site using the REST API. Specifically, I want to get the JSON data for my custom post type called 'news-post'. When I enter the URL hidden link, it returns 10 posts, but I don't see the French data.

Symptoms:
When using the REST API, I am unable to retrieve both English and French data for the 'news-post' custom post type.

Questions:
How can I get both the EN and FR data using the REST API?

February 11, 2025 at 8:14 am #16690222

Marcel
Supporter

Languages: English (English ) German (Deutsch )

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

Hi,

you can get them via /fr: hidden link.

[{"id":6996,"date":"2025-02-10T11:24:57","date_gmt":"2025-02-10T16:24:57","guid":{"rendered":"https:\/\/adisoke.ca\/?post_type=news-post&p=6996"},"modified":"2025-02-10T11:24:57","modified_gmt":"2025-02-10T16:24:57","slug":"mise-a-jour-sur-les-travaux-adisoke-10-fevrier-2025","status":"publish","type":"news-post","link":"https:\/\/adisoke.ca\/fr\/news-post\/mise-a-jour-sur-les-travaux-adisoke-10-fevrier-2025\/","title":{"rendered":"Mise \u00e0 jour sur les travaux : 

Best Regards,
Marcel

February 11, 2025 at 5:20 pm #16693104

paulM-12

Thank you Marcel! From the 2 json urls (hidden link + hidden link), is there any data that connects its secondary language post to the primary language post?
Example:
English post:
hidden link

Below is the French translation for the post above:
hidden link

From the json file, where does it show that French post is French version of that English post?

Thanks!

February 12, 2025 at 6:24 pm #16698608

Marcel
Supporter

Languages: English (English ) German (Deutsch )

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

Hi,

Not directly in the JSON, but in the database: WPML Tables Documentation.

The original ID is stored in the "trid" column. You can use the following hooks to retrieve the original ID or its translation:

- wpml_object_id – Retrieves the corresponding object ID in a specific language.
- wpml_element_trid – Gets the translation group (trid) for a given element.

So for your case to get the translated ID in the WP REST API, you can use wpml_object_id. This allows you to retrieve the corresponding translation of a post, page, or custom post type in the desired language. If you need the translated ID in the REST API response, you can modify the API output using a filter like rest_prepare_post. This way, when you request a post via the REST API, it will include the translated version’s ID based on the specified language.

Best regards,
Marcel