Home › Forums › ACF PRO › wp_insert_post() and populating ACF Blocks › Reply To: wp_insert_post() and populating ACF Blocks
I am answering the OP @stevekbristol
Yes, if it is a flex field you can dynamically insert rows using PHP.
I can’t give you specific details but I can explain what the flex field value will need to look like
// the flex field is a nested array
$flex_field_value = array(
// each nested array represents a row of the repeater
array(
// the "acf_fc_layout" is a special value that tells acf the layout name
'acf_fc_layout' => 'name_of_layout'
// the remaining values of this nested array contains field_key => value pairs
// for each field in the layout
'field_XXXXXXX' => 'value for sub field',
'field_YYYYYYY' => 'value for sub second field'
)
);
each field value in each row will need a value that matches the way that ACF stores that value in the DB. In addition to this, if you have nested repeaters (repeater, flexible content, and group fields) these fields will also be arrays with values for each row that is similar to the flex field.
Basically, this value needs to look identical to the value that is submitted by ACF fields when you click on the “update” button when editing a post.
Once you have the flex field array populated you can use update_field() to populate the flex field… using the field key of the flex field
update_field('field_ZZZZZZZZ', $flex_field_value, $post_id);
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.