Home›Support›English Support›[Waiting for user confirmation] Displaying translated fields in a Ninja table using WPML's icl_object_id function?
[Waiting for user confirmation] Displaying translated fields in a Ninja table using WPML's icl_object_id function?
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.
WordPress 6.7 has introduced a new issue that impact translations, please update WooCommerce and WPML to the latest versions before you report issues. More about this here - https://wpml.org/errata/php-error-wp-6-7-notice-function-_load_textdomain_just_in_time-was-called/
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: