Ask your WordPress questions! Pay money and get answers fast! (more info)

Advanced Custom Fields, Echo select field label as well as value WordPress


I am using the wordpress [[LINK href=""]]Advanced Custom Fields plugin[[/LINK]].

This question is about the [[LINK href=""]]select field[[/LINK]], and in the documentation only tells me how to output the value, not the label/name.

I can't see this functionally in the documentation and was wondering if anyone had any ideas?

For example, to create Select field content, you simple do this...

gb : English
fr : Français
it : Italiano
de : Deutsch
pt : Português
es : Español
nl : Nederlands
be : Belgian
dk : Dansk
fi : Suomi
no : Norske
cz : Český
pl : Polski
hu : Magyar
ch : Schweiz
at : Österreich
eu : International

Then to output this, you write this...

<?php the_field('language'); ?>

For example if I select Polski in my post editor, the PHP will echo this value...


I'm wondering if its possible to echo the label, I also want to be able to echo Polski, as well as the value pl.

Working answer gets prize amount. Thanks


Documentation updated by Elliot at ACF

I've just added the neccessary code to do this to the select documentation:


Answers (2)


Dbranes answers:

Hi, have you tried:
function get_field_label($field_name,$post_id = false)
global $post, $wpdb;
$post_id = $post->ID;
$query = 'SELECT meta_value FROM wp_postmeta where meta_key = "'.get_post_meta($post_id,"_".$field_name,true).'"';
$field = unserialize($wpdb->get_var($query));
return $field['label'];


Josh Cranwell comments:

I tried reading this post earlier but I could not understand how to get it to work in my case.

So if I put this in my functions, how do I echo the select label for my field?


Josh Cranwell comments:

Currently I am using this...

<?php echo get_sub_field('translation_language'); ?>

It is a sub field, not sure if this will cause an issue.

Dbranes comments:

ok, I checked the ACF database structure, and made this function that seems to work:

function get_field_label_mod($group_name,$field_name){
global $post, $wpdb;

$query_pid = 'SELECT meta_value FROM '.$wpdb->posts.' where post_title = %s';
$pid= $wpdb->get_var($wpdb->prepare($query_id,$group_name));

$query_field='SELECT meta_value FROM '.$wpdb->postmeta.' where meta_key = %s';
$field = $wpdb->get_var($wpdb->prepare($query_field,"_".$field_name));

$query_label = 'SELECT meta_value FROM '.$wpdb->postmeta.' where meta_key = %s';

$label = $wpdb->get_var($wpdb->prepare($query_label,$field));
$label = unserialize( $label);
return $label;

To use it




//get the label for 'gb'
echo $labels['choices']['gb'];
// this returns "English"

where you have to modify the two values <em>'group_name'</em> and <em>'field_name'</em>.

Dbranes comments:

another usage example for the above function:

// settings to EDIT

// get all the labels

// the current translation language key:

// the current translation language label:
echo $labels['choices'][$l];

this works on my install.

Dbranes comments:

ok Elliot's code works also for me ;-)

you should test:

$field = get_field_object('translation_language');
$value = get_field('lang');
$label = $field['choices'][ $value ];
echo "label:".$label;

(get_field_object is returning similar array as get_field_label_mod above)

Josh Cranwell comments:

OK I will do,

Thanks for you help dbraines!



Arnav Joy answers:

define following in functions.php

function get_selectbox_label($acf_field_name,$post_id = false,$selected_field)


global $post, $wpdb;



$post_id = $post->ID;


$query = 'SELECT meta_value FROM wp_postmeta where meta_key = "'.get_post_meta($post_id,"_".$acf_field_name,true).'"';

$field_data = unserialize($wpdb->get_var($query));

return $field_data['choices'][$selected_field];


and use it as

$your_acf_field_name = 'language';
$field_label = get_selectbox_label($your_acf_field_name,get_the_ID(),get_field($your_acf_field_name) );