The free version of the WordPress Polylang plugin does not include the language support in the REST API. With the assistance of ChatGPT-4o I added the following php-code at the end of the theme functions.php file :
add_action( 'rest_api_init', function () {
register_rest_field( 'post',
'translations',
array(
'get_callback' => function( $post_arr ) {
return function_exists( 'pll_get_post_translations' )
? pll_get_post_translations( $post_arr['id'] )
: null;
},
'schema' => null,
)
);
});
Here is the resulting JSON response when fetching a WordPress post in english, with translations in fr, de, pt and lb.
{"id":12,
"date":"2025-05-24T14:28:53",
"date_gmt":"2025-05-24T14:28:53",
"guid":{"rendered":"https:\/\/admin.ki-leierbud.lu\/?p=12"},
"modified":"2025-05-27T14:14:38",
"modified_gmt":"2025-05-27T14:14:38",
"slug":"ki-leierbud-what",
"status":"publish",
"type":"post",
"link":"https:\/\/admin.ki-leierbud.lu\/archives\/12",
"title":{"rendered":"KI-L\u00e9ierbud. What?"},
"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/admin.ki-leierbud.lu\/wp-content\/uploads\/what.png\" alt=\"\" class=\"wp-image-23\" srcset=\"https:\/\/admin.ki-leierbud.lu\/wp-content\/uploads\/what.png 1024w, https:\/\/admin.ki-leierbud.lu\/wp-content\/uploads\/what-300x300.png 300w, https:\/\/admin.ki-leierbud.lu\/wp-content\/uploads\/what-150x150.png 150w, https:\/\/admin.ki-leierbud.lu\/wp-content\/uploads\/what-768x768.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>KI-L\u00e9ierbud is an interactive web portal aimed at the general public, with the goal of making artificial intelligence (AI) accessible and understandable.<br>This site will offer educational content, interactive demonstrations, mini-courses, quizzes, and simulators to explain the fundamentals, applications, and ethical issues related to AI.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>KI-L\u00e9ierbud is an interactive web portal aimed at the general public, with the goal of making artificial intelligence (AI) accessible and understandable.This site will offer educational content, interactive demonstrations, mini-courses, quizzes, and simulators to explain the fundamentals, applications, and ethical issues related to AI.<\/p>\n",
"protected":false},
"author":1,
"featured_media":0,
"comment_status":"closed",
"ping_status":"closed",
"sticky":false,
"template":"",
"format":"standard",
"meta":{"footnotes":""},
"categories":[203],
"tags":[],
"class_list":["post-12",
"post","type-post",
"status-publish",
"format-standard"
"hentry","category-leierbud"],
"translations":{"en":12,"fr":71,"de":73,"lb":76,"pt":114},
"_links":{"self":[{"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/posts\/12",
"targetHints":{"allow":["GET"]}}],
"collection":[{"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/posts"}],
"about":[{"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/types\/post"}],
"author":[{"embeddable":true,"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/users\/1"}],
"replies":[{"embeddable":true,
"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/comments?post=12"}],
"version-history":[{"count":4,
"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/posts\/12\/revisions"}],
"predecessor-version":[{"id":437,
"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/posts\/12\/revisions\/437"}],
"wp:attachment":[{"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/media?parent=12"}],
"wp:term":[{"taxonomy":"category",
"embeddable":true,
"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag",
"embeddable":true,
"href":"https:\/\/admin.ki-leierbud.lu\/wp-json\/wp\/v2\/tags?post=12"}],
"curies":[{"name":"wp",
"href":"https:\/\/api.w.org\/{rel}",
"templated":true}]}}