Home›Support›English Support›[Resolved] Displaying translated fields in a Ninja table using WPML's icl_object_id function?
[Resolved] Displaying translated fields in a Ninja table using WPML's icl_object_id function?
This thread is resolved. Here is a description of the problem and solution.
Problem: The client created a custom post type with associated custom fields using ACF, translated via WPML. They then used Ninja Table to display these fields. While it works in French, in English, the custom field values still appear in French, and the shortcodes added to the table do not display any content.
Solution: We recommended modifying the function to dynamically retrieve the post type of the
$post_id
instead of hardcoding it. We also suggested adding error handling for missing fields and allowing dynamic shortcode attributes. Here is the revised code:
// Function to get translated ACF fields using WPML
function get_translated_acf_fields($post_id, $language_code = null) {
if (!$language_code) {
$language_code = apply_filters('wpml_current_language', null);
}
$translated_post_id = apply_filters('wpml_object_id', $post_id, get_post_type($post_id), false, $language_code);
if (!$translated_post_id) {
return null;
}
$fields = array(
'ville' => get_field('ville', $translated_post_id),
'publics' => get_field('publics', $translated_post_id),
'programmes' => get_field('programmes', $translated_post_id),
);
return $fields;
}
add_shortcode('ville_field', 'display_ville_field');
add_shortcode('publics_field', 'display_publics_field');
add_shortcode('programmes_field', 'display_programmes_field');
Additionally, we suggested testing the code without Ninja Table first and ensuring that Ninja Table is set as "Translatable" in WPML > Settings > Post Type Translation, then translating the tables and using their respective shortcodes in each language.
If this solution does not resolve your issue or seems irrelevant due to updates or differences in 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 further assistance is needed, please open a new support ticket at WPML support forum.
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.
Background of the issue:
I created a custom post type and associated custom fields to it using ACF. All these custom fields are translated using WPML. I then created a Ninja Table based on the custom post type to display the title of the post from that post type as well as a few custom fields (including two multiple choices ones). It works great in the main language (French), but not in English. In the English table, it links to the correct English post, but the custom field values appear in French. I added code in my theme's function.php to get translated ACF fields using WPML's icl_object_id function and created shortcodes for the fields. This is the code I used:
// Function to get translated ACF fields using WPML
function get_translated_acf_fields($post_id, $language_code = null) {
if (!$language_code) {
$language_code = apply_filters('wpml_current_language', null); // Get the current language
}
// Get the translated post ID using WPML's icl_object_id function
$translated_post_id = apply_filters('wpml_object_id', $post_id, 'communautaire', false, $language_code);
if (!$translated_post_id) {
return null; // Return null if no translation exists
}
// Retrieve the ACF fields for the translated post
$fields = array(
'ville' => get_field('ville', $translated_post_id),
'publics' => get_field('publics', $translated_post_id),
'programmes' => get_field('programmes', $translated_post_id)
);
return $fields;
}
// Shortcode for Ville field
function display_ville_field($atts) {
// Get the current post ID and language
$atts = shortcode_atts(
array(
'post_id' => get_the_ID(),
'language' => apply_filters('wpml_current_language', null)
),
$atts,
'ville_field'
);
// Get translated fields
$fields = get_translated_acf_fields($atts['post_id'], $atts['language']);
I then added the shortcodes in the table: [ville_field post_id="{post_id}" language="en"], [publics_field post_id="{post_id}" language="en"], etc. But nothing appears in those columns. Ninja Table does allow shortcodes and I tested a simple shortcode to see if it would display and it did.
Symptoms:
In the English table, the custom field values appear in French, and the shortcodes added in the table do not display any content.
Questions:
Am I using WPML's icl_object_id function correctly?
Is there a simpler workaround to retrieve and display data translated by WPML?
// Function to get translated ACF fields using WPML
function get_translated_acf_fields($post_id, $language_code = null) {
// Default to current WPML language if no language code is provided
if (!$language_code) {
$language_code = apply_filters('wpml_current_language', null);
}
// Retrieve the translated post ID
$translated_post_id = apply_filters('wpml_object_id', $post_id, get_post_type($post_id), false, $language_code);
// Return null if no translation exists
if (!$translated_post_id) {
return null;
}
// Retrieve ACF fields for the translated post
$fields = array(
'ville' => get_field('ville', $translated_post_id),
'publics' => get_field('publics', $translated_post_id),
'programmes' => get_field('programmes', $translated_post_id),
);
return $fields;
}
// Shortcode for Ville field
function display_ville_field($atts) {
// Parse shortcode attributes
$atts = shortcode_atts(
array(
'post_id' => get_the_ID(),
'language' => apply_filters('wpml_current_language', null),
),
$atts,
'ville_field'
);
// Get translated fields
$fields = get_translated_acf_fields($atts['post_id'], $atts['language']);
// Return the field value or a default message if not found
return esc_html($fields['ville'] ?? 'Ville field not found');
}
add_shortcode('ville_field', 'display_ville_field');
// Shortcode for Publics field
function display_publics_field($atts) {
// Parse shortcode attributes
$atts = shortcode_atts(
array(
'post_id' => get_the_ID(),
'language' => apply_filters('wpml_current_language', null),
),
$atts,
'publics_field'
);
// Get translated fields
$fields = get_translated_acf_fields($atts['post_id'], $atts['language']);
// Return the field value or a default message if not found
return esc_html(implode(', ', (array)($fields['publics'] ?? array())));
}
add_shortcode('publics_field', 'display_publics_field');
// Shortcode for Programmes field
function display_programmes_field($atts) {
// Parse shortcode attributes
$atts = shortcode_atts(
array(
'post_id' => get_the_ID(),
'language' => apply_filters('wpml_current_language', null),
),
$atts,
'programmes_field'
);
// Get translated fields
$fields = get_translated_acf_fields($atts['post_id'], $atts['language']);
// Return the field value or a default message if not found
return esc_html(implode(', ', (array)($fields['programmes'] ?? array())));
}
add_shortcode('programmes_field', 'display_programmes_field');
Improvements Made:
Dynamic Post Type Handling:
Dynamically retrieves the post type of the $post_id instead of hardcoding 'communautaire'.
Error Handling:
Added default messages for cases where a field is missing (Ville field not found, etc.).
Handles scenarios where get_field() might return null.
Dynamic Shortcode Attributes:
Allows passing custom post_id and language dynamically through shortcode attributes.
Debugging:
You can add error_log() statements in critical sections (like retrieving wpml_object_id and get_field) for debugging in development environments.
Default to Current Post ID and Language:
Uses get_the_ID() and apply_filters('wpml_current_language', null) as fallbacks.
Debugging Tips:
Enable WP_DEBUG and WP_DEBUG_LOG in wp-config.php to log errors: