Search code examples
wordpressadvanced-custom-fields

Wordpress: Fetch custom field value in the right language (WPML)


I created a custom field (using ACF Plugin) called "my_custom_field". Then I use this code to get the value for that custom field which works fine:

echo $output = get_post_meta($data['post_id'], "my_custom_field", true);

My site have two languages (french and spanish). I use WPML for translations.

Problem is that the $output always shows the value for the default language (french) even if the $output is loaded and printed in an "spanish" page.

I guess that I need to add something else in my code to detect the language and show the $output accordingly.


Solution

  • this fixed the problem:

    $wpml_post_id = icl_object_id($data['post_id'], 'page', false,ICL_LANGUAGE_CODE);
    echo $output = get_post_meta($wpml_post_id, "my_custom_field", true);