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

I am pulling some data from an API and everything works fine except for when I try to "insert" into one of the repeater fields. Here is a sample of the data being pulled as presented by print_r function

Array
(
    [speakers] => Array
        (
            [0] => Array
                (
                    [id] => 318880
                    [email] => test@test39.com
                    [first_name] => Jody
                    [middle_name] => 
                    [last_name] => Larose
                    [suffix] => 
                    [company] => The Empire Club of Mars
                    [job_title] => Executive Director (Interim)
                    [profile_picture] => https://assets.hello.com/uploads/medium/1457077-6183b6213d030.jpg
                    [bio] => Jody's Bio goes Here
                )

            [1] => Array
                (
                    [id] => 318878
                    [email] => hannah.pattison@empireclub.org
                    [first_name] => Hannah
                    [middle_name] => 
                    [last_name] => Pattison
                    [suffix] => 
                    [company] => Empire Club of Mars
                    [job_title] => Senior Manager, Events and Communications
                    [profile_picture] => https://assets.hello.com/uploads/medium/1288381-610d403d0b59b.png
                    [bio] => Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                )
        )
    [sponsors] => Array
        (
            [0] => Array
                (
                    [id] => 167781
                    [name] => CBA
                    [website] => https://cba.ca/
                    [description] => 
                    [logo_id] => //assets.hello.com/uploads/full/1263290-60f57896a2296.png
                    [notes] => 
                )
            [1] => Array
                (
                    [id] => 167783
                    [name] => Edelman
                    [website] => https://www.edelman.com/
                    [description] => 
                    [logo_id] => //assets.hello.com/uploads/full/1263300-60f5795750e25.png
                    [notes] => 
                )
        )
)

And here is an image from the how the two repeater fields are defined.

enter image description here

And here is the code for inserting the information from the API

function insert_events_from_api(): string {
    $event_post_data = array(
        'post_type' => 'event',
        'post_content' => '',
        'post_status' => 'publish',
    );
    $events = get_events_from_api();

    foreach ( $events as $event ) {
        if ( check_if_post_with_event_id_exists( $event['id'] ) ) {
            continue;
        }

        $event_modified = $event + array(
            'event_image' => '',
            'video_link' => '',
            'audio_link' => '',
        );

        $event_post_id = wp_insert_post(
            $event_post_data + array(
                'post_title' => $event['name'] ?? 'Unknown Event',
            )
        );
        $event_keys = array_keys( $event_modified );
        foreach ( $event_keys as $event_key ) {
            if ( 'id' === $event_key ) {
                update_field( 'event_id', $event_modified[ $event_key ], $event_post_id );
            } else {
                update_field( $event_key, $event_modified[ $event_key ], $event_post_id );
            }
        }
    }

    return 'EVENTS WERE INSERTED SUCCESSFULLY';
}

What I don't understand is the event speaker data is inserted correctly but for some strange reason, the event sponsor data is not?

Here is an SQL query am using to get the data from the db

SELECT 
  * 
FROM 
  wp_postmeta 
WHERE 
  post_id IN (
    SELECT 
      ID 
    from 
      wp_posts 
    WHERE 
      post_type = 'event' 
      AND post_status <> 'trash'
  ) 
  AND meta_key LIKE '%sponsor%' OR meta_key LIKE '%speaker%

And as you can see from the screenshot, the sponsors data looks serialized, but the speaker information is inserted correctly.

enter image description here

Could somebody help me out here, I don't what am missing?

Comments


  • Allure

    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);
    
  • Figure

    To make the ACF field translatable for WPML you need to install the ACFML plugin that you can download from your account on the WPML website.

    With this plugin, you can make ACF fields translatable. More you can read inside official documentation: https://wpml.org/documentation/related-projects/translate-sites-built-with-acf/

  • Insane

    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.

Add Comment