Home › Forums › General Issues › Issue with Displaying Labels Instead of Values for ACF Dynamically Populated Sel
Hi.
I have been working on creating a select field in Advanced Custom Fields (ACF) where the choices are dynamically populated based on a textarea input in a fixed page. I followed the guide on “Populating Choices From a Repeater Field” from the ACF documentation here: [https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/](https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/).
In the textarea, I’m inputting choices in the following format:
`
value_abc : LABEL_ABC
value_xyz : LABEL_ZETTN
`
When I edit the textarea to add choices, the labels appear correctly in the post edit screen and are selectable.
However, when the post is published and I use get_field()
to display the data, it shows the value instead of the label. This is not the expected behavior, as I would prefer the label to be displayed.
Could you advise on what might be missing or required in the theme or ACF settings to correctly display the label instead of the value?
As an additional note, after some trial and error, I found that exporting and then re-importing ACF settings seemed to correct the issue and the labels displayed as expected. I hope this information might be helpful in diagnosing the issue.
Thank you for your assistance.
You did not say, there is an option on the select field for return value that you need to set to “Label”. Did you do this?
@hube2 Yes.
I need both value and label, so I set Return format to Both.
What is the exact code you are using to display the value from get_field().
@hube2
Thank you for your prompt response.
Regarding the code used for display, it is executed via the WordPress REST API. Below is a simplified version of the code. Please note that it may look odd due to simplification, but essentially, it’s just using get_field to retrieve data. Since the select field works correctly with choices that were initially present and after performing an export/import, I believe there is no bug in this code.
<?php
add_action('rest_api_init', function () {
register_rest_route(
'v1',
'matches/(?P<slug>[a-z0-9_\-]+)',
array('callback' => 'matchesResponse')
);
});
function matchesResponse($request)
{
$post_args = [
'name' => $param_slug,
'post_type' => 'matches',
'post_status' => $request['post_status'],
'posts_per_page' => -1
];
$post_query = new WP_Query($post_args);
if ($post_query->have_posts()) :
while ($post_query->have_posts()) :
$post_query->the_post();
global $post;
$data[] = get_field('opponent', $post->ID);
endwhile;
wp_reset_postdata();
endif;
print_r($data);
return new WP_REST_Response($data);
}
Is this returning an array for each selection containing value => label pairs?
@hube2
The value-label pairs that were present from the start return as value => label. However, when one of the choices that I have added is selected, it returns as value => value.
Going to be honest, I haven’t got a clue why get_field() is not returning what you expect in this case.
I’m honestly just as puzzled. However,
the fact that exporting and then re-importing ACF settings seems to resolve the issue suggests that the cause might lie within the ACF mechanism itself. Given this, would it be better to contact the support team directly rather than posting on the support forum? (I am an ACF PRO user.)
When you say ACF settings, do you mean the field settngs or some other settings. I had thought that the issue might be the rest_api_format ACF setting, but looking in the code I could not find a reason this would affect your get_field() call.
But yeah, I would say that contacting the developers would be your best bet if this isn’t it.
When I referred to “ACF settings,” I meant the Export option available under Tools in the ACF section of the admin panel. I was specifically talking about exporting the Field Groups.
I’m considering setting up a small test site to share for verification purposes, but I’m not sure what the best approach is for that. For now, I plan to contact the developers.
I would like to keep this thread open as I am also waiting for responses from others.
Thank you for taking the time to look at my question.
@hube2
Thank you for your prompt response.
This is a screenshot of the ACF select field settings (note that it includes Japanese text). The choices for this select field are automatically updated when a separate textarea is modified.
As mentioned in my initial post, I am following the instructions provided here: ACF Guide on Dynamically Populating Select Fields.
Regarding the code used for display, it is executed via the WordPress REST API. Below is a simplified version of the code. Please note that it may look odd due to simplification, but essentially, it’s just using get_field to retrieve data. Since the select field works correctly with choices that were initially present and after performing an export/import, I believe there is no bug in this code.
<?php
add_action('rest_api_init', function () {
register_rest_route(
'v1',
'matches/(?P<slug>[a-z0-9_\-]+)',
array('callback' => 'matchesResponse')
);
});
function matchesResponse($request)
{
$post_args = [
'name' => $param_slug,
'post_type' => 'matches',
'post_status' => $request['post_status'],
'posts_per_page' => -1
];
$post_query = new WP_Query($post_args);
if ($post_query->have_posts()) :
while ($post_query->have_posts()) :
$post_query->the_post();
global $post;
$data[] = get_field('opponent', $post->ID);
endwhile;
wp_reset_postdata();
endif;
print_r($data);
return new WP_REST_Response($data);
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.