Just as a follow-up. Was able to create a new post on a user registration and pre-pop the flexible layout and data with update field. Here is the code I added to my functions.php file for any one else looking:
function create_new_user_posts($user_id){
// Create new post.
$post_data = array(
'post_title' => 'New Post Title',
'post_type' => 'Custom Post Name',
'post_status' => 'publish',
'post_author' => $user_id,
);
//Insert the post into the database
$post_id = wp_insert_post( $post_data );
// Save a flexible content field value.
$field_key = "field_5eb8736d02e62";
$value = array(
array(
'acf_fc_layout' => 'layout_1',
'field_5eb873ab02e63' => 'This is what I want the field to say'
),
array(
'acf_fc_layout' => 'layout_2',
'field_5eb8743702e68' => 'Starting 2 text'
)
);
update_field( $field_key, $value, $post_id);
}
add_action('user_register','create_new_user_posts');
Thank you for your help. Pushed me in the right direction.
If I am following you correctly, this is how it should look:
//Prepop Initial Notes
add_filter('acf/load_value', 'add_starting_flex', 1, 3);
function add_starting_flex($value, $post_id, $field) {
if ($field['key'] != 'field_5eb8736d02e62' || $value !== NULL) {
return $value;
}
// add default layouts
$value = array(
array(
'acf_fc_layout' => 'layout_note_text',
'field_5eb873ab02e63' => 'This is for the General Note only.'
),
array(
'acf_fc_layout' => 'layout_video_note',
'field_5eb8743702e68' => 'Starting Video',
)
);
return $value;
}
I tried that with no luck. Nothing was passed into the new post now. Did I get that incorrect?
Hey –
I have the exact same question and saw that this went unanswered. Did you ever figure it out?
Thanks!
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.