Home Forums General Issues Add data to ACF fields on post creation?


Add data to ACF fields on post creation?

  • Hi all,

    I am pulling data from an API and then creating a new post for each section of information pulled from the API.

    How can I update ACF fields for that custom post type on creation? Would I need to create the posts first so they have IDs and then loop through the newly created ones?

    Ideally of course it would be better to be able to do it all at once rather than looping twice.

  • You need to create the post first and then add the fields using the update_field() function to update each field. You will need to use the field key for $selector when doing this. You’ll also need to make sure that you’re using the right type of value depending on what type of field you are updating, so you’ll need to investigate how ACF stores the needed values or if there are any special requirements for the $value of a specific type of field.

    It might be possible to update it all at once using wp_insert_post(), however, this will require understanding how every type of field is actually stored in the database as well as making sure that you also add the correct field key references for every field. Doing it this way would be much more complex than using update_field()

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.