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.
There's no way to reliably retrieve the key using the name, because the name is metadata, and hence open to change, whereas the key (at least without editing the database manually) isn't.
However, this function will work with the most recent version of ACF, with a couple of caveats. ACF creates field groups within posts as a custom post type of ACF, but all the data about the fields themselves is held within the post_meta table.
A warning: because the field name is stored as part of an array, when finding it via SQL you have to rely on a wildcard search, which is open to errors. As long as all your fields have entirely different names, you're fine, but if for example you have one field called "farm" and another called "farmer", this will error because it will find both fields.
The only reliable way to update fields is to manually code the key, though this is admittedly clunky, which is what led me here to begin with.
Here is a modified version of answer provided by @BFDatabaseAdmin matching the exact meta_value in "LIKE"
ACF provides some easy ways to help keep multiple environments in sync - you can register your fields with PHP or a local JSON file. Doing this would allow you to keep using get_field_object() with a single field key across multiple environments. See:
http://www.advancedcustomfields.com/resources/register-fields-via-php/
http://www.advancedcustomfields.com/resources/local-json/
I usually do my ACF development with the user interface and then export all of my fields groups as PHP to deploy across multiple environments.
UPDATE with a simple example: You could add this to functions.php or a custom plugin to add your field to multiple environments programmatically .. then you call get_field_object() with a common field_key across all environments
Had same problem, also ended up using key which lead me just to another dead end with no normal way to get key value, but luckily encountered this.
Taken from - https://wordpress.stackexchange.com/questions/248006/acf-add-fields-values-to-newly-inserted-post
Where acf_updateValue simulates how its done by ACF itself when you manually save. Since only update_field is not enough for ACF columns
The way ACF works you really should use the key.
from (http://www.advancedcustomfields.com/resources/get_field_object/)
"You can and should use the $field_key 100% of the time.
The problem with using $field_name is that if the reference does not already exist, ACF will not be able to find the field object and will not be able to save the value. This situation would occur if you had used code to insert a post.
Also, it is more efficient to use the field_key as the first parameter in the update_field function as it bypasses the reference look up."
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.
Then I was able to use:
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.