I’m creating posts programmatically by inserting values from an API using update_field(). While I’m able to assign the values for the ACF fields in the post (of a custom post type), they don’t display on the frontend (the preview page) until I enter the “post edit page” and hit “Update”. The values do appear on the post edit page but don’t actually apply and display on the frontend until the post is updated once.
However, the issue described above only concerns text fields. Other fields that take an array as value seem to be working perfectly fine.
// Create new post. $post_data = array( 'post_title' => 'My post', 'post_type' => 'movies', 'post_status' => 'publish' ); $post_id = wp_insert_post( $post_data ); // Save a basic text value. $field_key = "parentfield_subfield"; $value = $some_api_string_value; update_field( $field_key, $value, $post_id );
It should probably also be noted that instead of using field_key: “field_1234”, I’m using “parentfield_subfield”. I wasn’t able to get it working by using the pattern “field_1234”. I assume you don’t have to specify the parent field or anything like that, since field keys are unique? I even tried “parentfield_1234_subfield_1234” without success.
I did read John’s highlighted answer here, but didn’t fully grasp the reasoning of using “field_1234” at the time, but is this perhaps one of the issues that you get by not using it?
John Huebner August 21, 2017 at 5:03 am:
It is usually always better to use field keys vs field names for updating fields. The reason for this is for fields that do not yet exist because ACF will not know what type of field the field name refers to when the field does not exist yet.
If you are updating a sub field of another field then you need to supply an array when updating the field.
Assuming that you are using a group field due to the lack of index values between “parent_field” and “sub_field”. If it’s not a group field and you’re using a repeater let me know and I will give you the correct array structure for repeaters.
$value = array( // subfield key => value pairs 'field_XXXXX' => 'value', 'field_YYYYY' => 'value' ) // where field_AAAAA is the parent field update_field('field_AAAAAA', $value, $post_id);
You must be logged in to reply to this topic.
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!