Skip to content

Cache returns wrong translation for select choice #244

@slackday

Description

@slackday

This might be complex but I would like to know more how acf:cache works so I can look for a solution. It seems the wrong value is returned for "choices" when page is translated using wpml.

I have defined the choices field with addSelect like this

            ->addSelect('gender', [
                'label' => 'Kön',
                'choices' => [
                    'mare' => _x('Mare', 'Gender select', 'sage'),
                    'stallion' => _x('Stallion', 'Gender select', 'sage'),
                    'gelding' => _x('Gelding', 'Gender select', 'sage'),
                ],
                'default_value' => 'stallion',
                'wpml_cf_preferences' => 1,
            ])

wpml_cf_preferences = 1 is so that the value of the select is copied to the translation.

I return the value in my template like this

        $gender = get_field('gender', $post_id);
        if (! empty($gender)) {
            $gender_object = get_field_object('gender');
            $gender = $gender_object['choices'][$gender] ?? $gender;
            $details['gender'] = [
                'label' => __('Gender', 'sage'),
                'value' => $gender,
            ];
        }

This works but if I change language on my site the untranslated value is returned.

If I run wp acorn acf:clear the correct value is returned. But running wp acorn acf:cache gives the wrong value again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions