Skip Navigation

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.

Tagged: 

This topic contains 0 replies, has 2 voices.

Last updated by Andreas W. 1 week, 2 days ago.

Assisted by: Andreas W..

Author Posts
November 12, 2024 at 9:17 pm #16394695

Mary

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']);

return esc_html($fields['ville']);
}
add_shortcode('ville_field', 'display_ville_field');

// Shortcode for Publics field
function display_publics_field($atts) {
$atts = shortcode_atts(
array(
'post_id' => get_the_ID(),
'language' => apply_filters('wpml_current_language', null)
),
$atts,
'publics_field'
);

$fields = get_translated_acf_fields($atts['post_id'], $atts['language']);

return esc_html(implode(', ', (array)$fields['publics']));
}
add_shortcode('publics_field', 'display_publics_field');

// Shortcode for Programmes field
function display_programmes_field($atts) {
$atts = shortcode_atts(
array(
'post_id' => get_the_ID(),
'language' => apply_filters('wpml_current_language', null)
),
$atts,
'programmes_field'
);

$fields = get_translated_acf_fields($atts['post_id'], $atts['language']);

return esc_html(implode(', ', (array)$fields['programmes']));
}
add_shortcode('programmes_field', 'display_programmes_field');

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?

November 18, 2024 at 10:17 pm #16416853

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

Take note that this is a custom code request and such requests are usually not supported on this forum.

WPML Suppport Policy:
https://wpml.org/purchase/support-policy/#:~:text=WPML%20Support%20Policy%20If%20you%20have%20a%20valid,you%20to%20get%20the%20most%20out%20of%20WPML.

Could you please give this a try?

// 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:

Enable WP DEBUG in wp-config.php:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);

Check the logs in wp-content/debug.log.

Add error logs inside get_translated_acf_fields() to confirm correct behavior:

error_log("Post ID: $post_id, Language: $language_code, Translated ID: $translated_post_id");

If you need further asistance with this issue, I suggest you consult one of our contractors:
https://wpml.org/contractors/

We hope for your understanding.

Best regards
Andreas

November 21, 2024 at 11:58 am #16428794

Mary

It didn't work, but since this is not covered by your support, I will try to find another solution.

November 21, 2024 at 2:36 pm #16429645

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

I would suggest you test the code without a Ninja Table first.

If each Ninja Table is displayed with a shortcode then each Ninja Table has its proper ID.

You will need to set Ninja Table as "Translatable" inside WPML > Settings > Post Type Translation and then translate the tables.

Each table will then needed to be placed into each content in each language using their respective shortcodes.