You might try using the load_field hook, http://www.advancedcustomfields.com/resources/acfload_field/
You could test to see if it’s a new post and if not then set some default values based on other default values. I’m not exactly sure what you’re looking for though. If it’s a new post then wouldn’t you just use default values for these fields? Are you talking about when a new post page is first loaded or when you first publish a post?
Try changing all of your acf function calls to include the post ID, but not this is not necessary for
// change this if( have_rows('bag_contents', $post_id) ): while ( have_rows('bag_contents', $post_id) ) :
// don't change this $produce = get_sub_field('produce');
// change this update_field('total_servings_per_week', $per_week, $post_id);
And I think you should only need to add your action once at priority 20
I made the updates. But the problem still remains. I forgot to mention, this form is a front end form. Do you think that has something to do with it?
I noticed that fields update like they should when I create a post in wordpress backend. Could that be the issue?
You’re creating a new post on the front end, that’s a different story. You might need to have a pre save post filter. http://www.advancedcustomfields.com/resources/acf-pre_save_post/
What do your arguments for acf_form() look like for creating the new post? Are you including arguments for the new post?
Take a look at the Creating a New Post section of the acf_form() documentation http://www.advancedcustomfields.com/resources/acf_form/
My guess is that the values are being added to whatever post or page your front end form for the new post form is on.
Turns out I was referencing the wrong template. My acf_form() did have the correct arguments.
I solved the problem by using field keys instead of field names and adding $post_id to the update_field function.
E.g. update_field(‘field_55760114e5887’, $per_week, $post_id);
Thanks a lot for helping me troubleshoot the problem though!
That makes sense. Sometimes I forget about the basics. Yes, when adding new posts you must use the field key in update_field(), which is explained here http://www.advancedcustomfields.com/resources/update_field/. Glad you got it worked out.
I don’t think you can trigger all posts to be updated, at least not easily.
I suppose, if you built a function that got all posts in you post type and called
do_action('save_post', $post_id); or
do_action('acf/save_post', $post_id); that might cause it, but I don’t know what other side effects there may be or even if it will work.
The topic ‘Add content to ACF Field when post is created not just when saved/updated’ is closed to new replies.
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!