Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Advanced Custom Fields field key from WordPress database?

Tags:

I’m using Advanced Custom Fields 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.

like image 718
user1432966 Avatar asked Jan 17 '14 10:01

user1432966


People also ask

How do I get advanced custom field value in WordPress?

Because ACF will format the value depending on the field type and make development quicker and easier! To retrieve a field value as a variable, use the get_field() function. This is the most versatile function which will always return a value for any type of field.

How do I get ACF field label?

You can output the label of an Advanced Custom Field: ACF field by using the get_field_object() function which stores an array of data about the custom field. /* * Output Label from ACF field */ $field_name = "acf_field_name_here"; $field = get_field_object($field_name); echo '<h3>'. $field['label'] .


1 Answers

Just trying to do this myself so I did some investigation. Seems each field and field group for ACF are stored in the wp_posts table in the database as custom post types. fields are 'acf-field' and groups are 'acf-field-group'.

I was able to use this function to get the field key to then use update_field($field_key, $value) on posts that didn't have the field already.

function get_acf_key($field_name){     global $wpdb;      return $wpdb->get_var("         SELECT post_name         FROM $wpdb->posts         WHERE post_type='acf-field' AND post_excerpt='$field_name';     "); } 

Then I was able to use:

update_field(get_acf_key('my_field_name'), 'some value', $post_id); 

To either update the field for posts that had it already or add the field and it's key reference to posts that did not already have the field.

like image 177
aaron.cimolini Avatar answered Sep 21 '22 10:09

aaron.cimolini