4

I have some custom post type "video" and I added some custom ACF fields to it ("video_path", "author_name" and "audio_author"). I'm generating posts in that type programmatically like this:

$video_post_params = array(
  'post_title'    => wp_strip_all_tags($video_title),
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_type'     => 'video'
);
$video_id  = wp_insert_post( $video_post_params );
update_field('video_path', $video_path, $video_id);
update_field('author_name', $video_author, $video_id);
update_field('audio_author', $audio_author, $video_id);

All values are inserted well - when I open the post in back-end everything is fine. However, when I try to use those values, I don't get anything?!?

I'm reading values from template files like this:

get_field('video_path', $video_id)

And if I open the post and just save it without any change everything starts working normally and I'm getting post ACF fields normally after that. Posts created manually, from back-end are working well all the time.

What I'm doing wrong? Do I need some extra step when generating posts from code?

The issue is reported here: http://support.advancedcustomfields.com/forums/topic/programmatic-post-insertion-acf-fields-and-the-save_post-hook/

But that solution is obviously not working for me - my update_field() functions already are immediately after wp_insert_post().

MilanG
  • 6,994
  • 2
  • 35
  • 64

4 Answers4

6

Found it!

When inserting ACF field value field key must be used. If key name is used instead, as I did, everything is inserted well at first look, but value isn't available until post is saved manually. So it's like:

update_field('field_56e683ab6265f', $video_path, $video_id);
update_field('field_56e68415b5c4b', $video_author, $video_id);
update_field('field_56e6842d58740', $audio_author, $video_id);

What a mess....

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • Dear God :D really what a mess, was stuck on this exact stuff for 3 hours tnx much for sharing! – Jiro Matchonson Dec 04 '18 at 15:33
  • 1
    Nice one but unfortunately it doesn't work with sub-fields inside a Group field because "The Group field uses both the parent and child field names when saving and loading values. For example, a Group field named ‘hero’ with a sub field named ‘image’ will be saved to the database using the meta name ‘hero_image’." (see: https://www.advancedcustomfields.com/resources/group/). Do you have a clean workaround? – Patrick Apr 21 '20 at 10:22
1

If you want to use the field name instead of the field key, you can use add_post_meta

For example:

add_post_meta($video_id, 'video_path', $video_path, true);
add_post_meta($video_id, 'author_name', $video_author, true);
add_post_meta($video_id, 'audio_author', $audio_author, true);
rdm
  • 658
  • 5
  • 16
  • how would this work? I'm pretty sure it wont. You also need to update the field key. – FooBar Oct 09 '19 at 14:33
  • @FooBar This assumes that video_path, author_name and audio_author are valid field keys. (Or, if you meant something else, like maybe you are thinking about using this in the context of a repeater, maybe expand what you are saying?) Note also: the fourth parameter (true) means that this will replace a previous value (if any). – rdm Oct 18 '19 at 22:04
  • This method is a good trick to deal with sub-fields inside of a `group` field, because you're supposed to concatenate the parent field name with the child field name to update them (e.g. : `update_field('parentfldname_childfldname', $value, $post_id);` ). I would suggest using `update_post_meta()` instead of `add_post_meta()` for more flexibility. – Patrick Apr 21 '20 at 09:28
-1

With ACF5 you have to use not post id, but post object, lake that:

update_field('field_56e683ab6265f', $video_path, $video);
update_field('field_56e68415b5c4b', $video_author, $video);
update_field('field_56e6842d58740', $audio_author, $video);
Leo Lukin
  • 1,201
  • 1
  • 11
  • 24
-1

I had the same problem, and I correct it with simply add do_action('acf/save_post', $postID); at the end of the script, and that's all…