Home › Forums › General Issues › How to get field key from database?
I’m using ACF with post-type. I have some select custom fields, and I want to show all the label choices from each field.
I’ve tried this way.
$field = get_field_object('hair_color');
$hair = $field["choices"];
foreach($hair as $value){
Doing a var_dump($field) it appears empty:
array(18) { ["key"]=> string(16) "field_hair_color" ["label"]=> string(0) "" ["name"]=> string(10) "hair_color" ["_name"]=> string(10) "hair_color" ["type"]=> string(4) "text" ["order_no"]=> int(1) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(20) "acf-field-hair_color" ["class"]=> string(4) "text" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["allorany"]=> string(3) "all" ["rules"]=> int(0) } ["default_value"]=> string(0) "" ["formatting"]=> string(4) "html" ["maxlength"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["value"]=> bool(false) }
The only way to get it full is that:
get_field_object('field_51ac9d333d704');
array(17) { ["key"]=> string(19) "field_51ac9d333d704" ["label"]=> string(13) "Color de pelo" ["name"]=> string(10) "hair_color" ["_name"]=> string(10) "hair_color" ["type"]=> string(6) "select" ["order_no"]=> int(9) ["instructions"]=> string(27) "Selecciona el color de pelo" ["required"]=> int(0) ["id"]=> string(20) "acf-field-hair_color" ["class"]=> string(6) "select" ["conditional_logic"]=> array(3) { ["status"]=> int(0) ["rules"]=> array(1) { [0]=> array(3) { ["field"]=> string(19) "field_5195ef9879361" ["operator"]=> string(2) "==" ["value"]=> string(5) "small" } } ["allorany"]=> string(3) "all" } ["choices"]=> array(5) { ["bald"]=> string(5) "Calvo" ["brown"]=> string(8) "Castaño" ["brunette"]=> string(6) "Moreno" ["red"]=> string(9) "Pelirrojo" ["blonde"]=> string(5) "Rubio" } ["default_value"]=> string(0) "" ["allow_null"]=> int(1) ["multiple"]=> int(0) ["field_group"]=> int(90679) ["value"]=> bool(false) }
But I have 3 environment, and I don’t want to hardcode the field key.
Is there any solution?
Thanks in advance.
Hi @funack
If the value has not been saved to the post via ACF, then the reference key does not exist. This means ACF has no way of finding the field object from just the name.
Sorry, but this is not possible.
Thanks
E
Hi,
I have a very similar problem. I’m currently using:
'post_content' => $_POST['fields']['field_53701c4623c64'],
to pass the value of a custom field I called ‘content’.
Is there any way to call the field key from the database using the field name? Or to set the field name as the input tag name in the DOM instead of the field key? As @funack mentioned using the field key requires hardcoding when using different environments.
Also, in case this is not possible, if I include ACF within my WP Theme will any custom field keys I create be kept? (so other users can plug and play?) I don’t think it will be because it gets the values from the database or is this not the case?
I would appreciate any help.
Thanks
Carlos
Sorry, please disregard my previous post.
I’ve been doing some reading on adding ACF to my theme and what I’m asking on my previous post sounds quite silly now.
Thanks for the amazing plugin!
The topic ‘How to get field key from database?’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.