Map Multiple Images During Post Creation with The Advanced Custom Fields Gallery
If you’re a regular reader of our blog, then you know that we love the Advanced Post Creation Gravity Forms add-on. It offers a combination of flexibility and ease-of-use that makes it dead simple to create posts and map meta fields. When combined with Advanced Custom Fields, you can create posts for any situation.
One of the most powerful features of ACF Pro is the Advanced Custom Fields Gallery field. It provides an interactive interface for managing a collection of attachments. Those attachments can then be easily displayed in your post by adding some code to your theme.
Combine this functionality with the Advanced Post Creation add-on and you can map a multi-file upload field to an ACF Gallery to easily attach images to the gallery when creating a post.
But what if you also need to select a featured image?
Typically, you’d have to create a separate File Upload field to handle that, bringing additional complexity in your form and an extra burden on your users.
In this tutorial, we’ll show you to use a single Multi-file Upload field to add a Featured Image to your post and map the rest of the images to an ACF Gallery.
Before getting started with your form, make sure to create a Gallery using Advanced Custom Fields Pro. Make sure to note the Field Name for that field in the ACF field settings. For our tutorial, we have given it the name
Step 1 – Add Fields to Your Form
You will need a minimum of three fields to create a post and upload images to the gallery: Single Line Text, Paragraph, and File Upload. Add each of these fields to your form, and give them appropriate names.
Step 2 – Activate Media Library
In the File Upload field’s settings, navigate to the Perks tab and check Upload to Media Library. This will upload the files to the media library, and when coupled with the APC settings in the next step, makes sure they’re properly mapped to the ACF Gallery field.
Step 3 – Create APC Feed and Map Fields
With the form set up, the next step is to create your Advanced Post Creation Feed and map the fields. Insert the merge tags for the Post Title and Post Body fields into the Title and Content fields respectively.
Under Custom Fields, you will map the Gallery field to two different custom fields:
gallery(or whatever you named your ACF Gallery field).
You can select
gallery from the list, but
_thumbnail_id will need to be typed in after selecting Add New Custom Field Name from the drop down.
That’s all there is to it. Once the fields are mapped, any new submission will automatically create a new post. The first image in the File Upload field will be set as the post’s Featured Image and the subsequent images will be placed in the ACF Gallery field.
Here you can see the post from the above submission as it appears in the Block Editor with the featured image and the gallery populated.
Taking It Further
Sorting Upload Files
The first image in the File Upload field is mapped to the Featured Image, which makes this a perfect fit for File Upload Pro’s sorting. With sorting enabled, users can rearrange uploaded files to make sure the correct image is used as the feature image for the post.