Home › Forums › General Issues › get_field() doesn't return a value when fields are created with update_field() › Reply To: get_field() doesn't return a value when fields are created with update_field()
1) Let’s say that the field name is “field_name”. When you use update field using the field name ACF looks in the database for a matching “_field_name” in order to get the field key. Since the field has never been inserted before it does not find one and does not know how to treat the value. Since it does not know what field key to use it does not insert any.
2) The value for “field_name” gets inserted into the database but the matching “_field_name” entry with the field key value does not because ACF does not know what it’s supposed to be. When you open the editor ACF finds the value for “field”name” and shows that value. ACF will do the same thing for per-existing custom fields. Let’s say you created a custom field using some other method and named it “some_other_field_name”, if you add this field name to a field group that is attached to the post where this value exists, the first time that you edit the post with that custom field ACF will convert it to an ACF field and add the proper field key to the database.
3) When you save the post again ACF saves the matching “_field_name” entry with the correct field key because at this point it knows that the field key is supposed to be.
While field names are important and let you get values using them, ACF does not actually use the field name by itself. When you use
get_field('field_name‘);` ACF looks up that field key for that field that is associated with the post. It it does not find the field key then it does not know how to treat the value. Sometimes it will work but in a lot of cases you will not get what you expect to get because you’ll just get whatever value is stored in the database. For example, if you have an image field, the ID of the attachment is stored. If ACF can’t find the field key then it does not know that you want to get an image array and just returns the ID value.
Hope that clears things up for you
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!