February 2, 2023: Added
post_date
parameter for updating the post date.
This article requires the GP Populate Anything perk.
Buy Gravity Perks to get this perk plus 46 other premium Gravity Forms plugins!
Overview
Using Gravity Forms to create posts is easy. With a Basic or Pro license you can use Post fields to create new posts on your site. With an Elite license you gain access to the Advanced Post Creation add-on, which can create custom posts, map custom fields, and add uploaded files to the Media Library.
One thing that neither of these methods can do is update existing posts. Post fields and the APC add-on can only create new posts. In this tutorial, we’re going to show you how to pair a snippet with Populate Anything to update existing posts in WordPress. Let’s get started.
Getting Started
Prerequisites
Confirm that you have Gravity Forms and Populate Anything installed and activated and that you’ve installed the snippet.
Step 1 – Add Post Field
Start by adding a Drop Down field to the form called “Post”. Use Populate Anything to populate posts into the Drop Down for the user to select from.
These settings will populate all Posts as choices in the Drop Down. If you want to limit the available Posts to ones that current user wrote, add a filter where Author is Current User.
Step 2 – Add Class and Parameters
With all of the fields in place, the next step is to add the following class to your theme’s functions.php.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'title' => 2,
'content' => 3,
'author' => 4,
'status' => 5,
'terms' => array( 6, 7 ),
'meta' => array(
'custom_field' => 8,
'another_custom_field' => 9
),
'featured_image' => 10,
'post_date' => array(
'date' => 11,
'time' => 12
),
) );
The snippet accepts the following parameters:
– form_id integer required
The ID of the form which will be used to update posts.
post_id integer required
The ID of the field whose value contains the Post ID.
title integer optional
The ID of the field whose value contains the new post title.
content integer optional
The ID of the field whose value contains the new post content.
author integer optional
The ID of the field whose value contains the new post author.
status integer optional
The ID of the field whose value contains the new post status.
terms array optional
An array of field IDs whose value contain an array of taxonomic term IDs.
meta array optional
An array of custom fields whose keys match the custom field’s name and values match the IDs of the field containing the custom field’s value.
featured_image integer optional
The ID of the field whose value contains the new featured image.
post_date integer|array optional
The ID of either the Date or Time field. When set with an array it accepts date and time keys, and the value set is to the field IDs of the date and time fields, respectively.
delete_if_empty boolean optional
When set to true, any post property with a mapped parameter and no submitted value will be removed. Currently only supports
featured_image
parameter. Defaults to false.
Using the Snippet
Update Title Field
Update the post title using the title
parameter. To populate the current title, first add a Single Line Text field to the form named “Post Title”. Then, use Populate Anything to populate the Post Title from the selected Post in the Drop Down.
This field populates with the selected post’s title. If the user replaces that title with a new title, the snippet will overwrite that title when the form is submitted. Map this field to the title
parameter in the snippet.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'title' => 2,
) );
Update Post Content
Use the content
parameter to update the post content. To populate the current content into the form, add a Paragraph field called “Post Content” and use the following Populate Anything settings.
Since WordPress posts contain HTML for formatting, I recommend activating Gravity Forms’ Rich Text Editor in the Advanced tab. Otherwise, the raw HTML will display on population, which can be confusing for the user.
Map this field’s ID to the content
parameter in the snippet.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'content' => 3,
) );
Update Post Author
The post author can be updated using the author
parameter. Use Populate Anything to populate a list of users into a choice field.
Then, populate the current post’s author as the value of that field. When the form loads, all user’s will be populated as choices in the field and the current post’s author will be selected from the options.
Set the author
parameter to this field. A user can select a new author from the options, and the snippet will take care of the rest.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'author' => 4,
) );
Update Post Status
The post status can be updated using the status
parameter. Create a choice field and set the choices to the available statuses and use Populate Anything to populate the currently selected post’s status into the value of that field.
Set the status
parameter to this field. When a new status is selected from the options, it will be updated on submission.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'status' => 5,
) );
Update Custom Fields
The snippet also supports updating custom fields using the meta
parameter. This parameter expects an array with the keys set to the name of each custom field and values set to the field ID that contains the desired value.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'meta' => array(
'custom_field' => 8,
'another_custom_field' => 9
),
) );
Update ACF Image Fields
Updating Advanced Custom Fields image, gallery, and file fields is supported via Gravity Forms Media Library using the meta
parameter. First, activate Media Library on a File Upload field.
Then, find the ACF custom field keys in the Field Group table. The key is located under the Name heading.
Update the meta
parameter array with the key set to the name of the ACF image field value set to the field ID for the File Upload field.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'meta' => array(
'band_picture' => 8,
),
) );
Update Featured Image
Featured Images can be updated via Gravity Forms Media Library using the featured_image
parameter. First, add a Single File Upload field to the form and activate GF Media Library on that field.
Set the featured_image
parameter to that field. When an image is uploaded to the field, GF Media Library will add it to WordPress media library and the snippet will attach it to the post as the featured image.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'featured_image' => 10,
) );
Update Categories and Terms
Update the post’s categories and terms using the terms
parameter. Flat and hierarchical terms are both supported, as well as custom terms. Use Populate Anything to populate a Checkbox field with the available terms.
Then, populate the current post’s terms as the value for the field. When the form loads, all available terms in the selected taxonomy will be populated as choices in the Checkbox field and the current post’s terms will be selected from the options.
Set the terms
parameter to an array of all terms fields. Multiple fields can be set, allowing you to set terms for multiple taxonomies with a single entry. A user can select new terms from the options, and the snippet will take care of the rest.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'terms' => array( 6, 7 ),
) );
Update Post Date and Time
The post date and time can be updated using the post_date
parameter. This parameter accepts an array with date
and time
keys, each mapped to a Date or Time field on the form respectively. Either can be omitted but at least one most be provided.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'post_date' => array(
'date' => 11,
'time' => 12,
),
) );
This parameter also accepts a single integer representing the ID of a Date or Time field. If the provided ID is for a Date field, the time is set to midnight. If a Time field ID is provided, the date is set to the current date.
new GW_Update_Posts( array(
'form_id' => 123,
'post_id' => 1,
'post_date' => 11,
) );
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 46+ essential add-ons for Gravity Forms with support you can count on.
If I wanted to reuse this for multiple post types, would I just create a second array inside new GW_Update_Posts. For example:
Hi Justin,
You’ll need to create multiple copies of the configuration. You can read more about that here. https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Best,
Will this snippet work for updating custom post type posts? For example I have a custom post type called Items that I would like to make updating on the front end possible. I’ve tested with the snippet and I cant seem to get the custom post type post to update.
The snippet uses the Post ID to identify the post to update, so it should work on updating custom post-type posts. Can you confirm if you’ve correctly set the Post ID field ID in the configuration? If it’s still not working, you can contact us via our support form if you have an active Gravity Perks license so that we can look at your setup and assist you.
Best,
Works great, thanks a lot for the amazing work!
You’re welcome. Nice to know the snippet works for you.
Best,
This is an awesome snippet, thanks ! How does it works if I want to create a new ACF repeater row and fill the subfields with the GF entry values at each form submission ?
Hi Guilluame,
Sorry, but it currently doesn’t support updating the ACF Repeater field. I’ll submit this as a feature request.
Best,
Hey, I’ve built this into a form on the page that I want the end users to be able to edit and it works great but I realized that when the title is edited the slug doesn’t change. Is there a way to edit this as well?
Hi Sean,
To edit the slug, you’ll need to set the ‘slug’ parameter to the ID of the field with the value you want to use.
new GW_Update_Posts( array( 'form_id' => 123, 'post_id' => 1, 'slug' => 3, ) );
Best,
Will this snippet let me …
I’m using Gravity Forms Advanced Post Creation to create posts when a user submits a form.
I’m using CUSTOM POSTS to take those entries and create a custom post called SINGERS (youth members of our choir). There are some taxonomies created by inputs on the form. For example, their school grade.
The entries create a member directory, which I’m using SEARCH & FILTER to be able to filter my members by grade, ensemble/group, etc.
When the user submits their form, however, they don’t know which group/ensemble they’re going to be in.
I can assign them to a group/ensemble (taxonomy) through my “ALL SINGERS POST” page. This group/ensemble field (and others) are an ADMIN dropdown field on my form.
If I change a taxonomy on my POST, will THIS snippet update the original Gravity Forms ENTRY with that new taxonomy? Specifically, if I update a Singer post with their ensemble name, will that ensemble name be saved to the Gravity Form Entry?
Thank you.
Hi Keith, we’ve already followed up via email. From what I understand you would like to update an Entry when updating the Post. This is not currently supported. The snippet will update the Post when the Entry is updated but not the other way around. Best,
Hello, what If I want to update the custom fields from the address field?
f.e: I have 3 fields: Street City Postal code, But I have only one field for address? Is it possible to accomplish that or do I need to change the fields one by one?
Thanks
You can specify the sub-fields from the address field using the same convention that merge tags do. For example, if my address field ID is 3, then it would look like this
You must put single quotes around the sub-field IDs for the snippet to understand them.
This is great, we would love to see this released as an official perk! If mapping fields were available inside the settings of each form that would be AMAZING! I’ve seen alternatives like this but no one does it like gravity wiz =D
Thanks! I’ll pass this along to our product manager. Currently, we aren’t planning on releasing this as a Perk because Gravity Forms intends to add this functionality to the APC Add-on.
@Scott. Do you know when Gravity Forms is planning on releasing this feature?
Hi Keith, not what we know. You can reach to Gravity Forms support in case they have an ETA about the feature.
If I were to embed this form on the post itself, how can I get the ID from the post. I know I can add a hidden field and pre-populate it with the post ID and map it to
post_id
, but that seems a bit insecure. Is there a way to get the post id behind the scenes?Thank you A.
Hi Adrian,
We’ve already followed up via email. This should be possible using Populate Anything and filtering by the current Post ID.
Cheers,
Great tutorial which I’m trying to adapt to help me solve a problem I have regarding inventory. I have a form which has a choice field dynamically populated from a custom post type where I have a number of course dates and spaces available as separate meta fields. The user can select up to 6 dates and I need to be able to update the custom post spaces available value as each dates is purchased. Is this possible using this method as I’m struggling to work out how to get the inventory value?
Hi Kenny,
This will depend on how you’ve set up the inventory for the custom post. At the moment, the solution that’s coming to mind will require some additional custom codes, but we’ll need more information on how the inventory is set up in the Custom Post. I’m going to send you an email to request this information.
Best,
This looks like exactly what I need! We’re using GF’s APC addon (with 3 GWiz perks) to add woocommerce products from the front end in a wc vendors marketplace. I followed this example of yours exactly, including fancy image uploads. Adding products works perfectly. However, I’m trying to give the user the ability to edit their uploads…
So I duplicated the form above, and deleted the APC feed. I added the snippet in a site specific plugin. I added the form-specific values to the functions.php. However, I cannot get the fields to pre-populate for love nor money. (I made a short blank form as per this tutorial, and it populates the fields perfectly.)
Is it not possible to duplicate an existing form and use this method of editing products?
Cheers,
Austen
Sorry – it wasn’t clear that I used this tutorial to make the first form to add the product in the first place: https://gravitywiz.com/how-to-create-woocommerce-products-with-gravity-forms/
Then I used this tutorial to try to make the edit form by cloning the above one and removing the APC feed before following the instructions here.
Hi Austen,
In other to pre-populate the field, you’ll have to use our GP Populate Anything Perk to set up all the fields to be populated with values from the Post, either based on the current Post or a selected Post ID. We’ll want to take a look at your form to get a better picture of your setup, so I’ll be sending you an email to request more information.
Best,