February 2, 2023: Added
post_date
parameter for updating the post date.August 5, 2021: Improved
delete_if_empty
argument by adding support for custom fields.
This article requires the GP Populate Anything perk. Buy Gravity Perks today 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 45+ essential add-ons for Gravity Forms with support you can count on.
I am successfully mapping sub-fields for the address field using the sub-field ID. ‘street-address’ => ‘9.1’
This same setup is not working for the Name field. ‘first-name’ => ‘1.1’, ‘last-name’ => ‘1.2’,
How would I map the Name field to these custom fields?
Hi Chris,
The modifiers for the first name and last name are .3 and .6 respectively so you would have something like so, first-nameโ => โ1.3โ, โlast-nameโ => โ1.6โ,
Here’s the documentation on the modifiers for the Name field.
Best,
Thank you, Samuel. That worked.
Is there a way to add conditional logic in the functions.php code to map different question ID’s to the custom fields based on an answer to a question? For example, if the country is Canada, we want to map different ID’s to the custom fields than if the country is USA.
Hi Chris,
This will definitely require some customization to the snippet or some changes in the form setup. Since you’re a Pro customer, I’m going to send you an email to request more information via email so we can see if we can assist you with a solution.
Best,
Hello,
I made a form for users to create posts using APC and I just created a second form for the user to update their post using your method as explained here. The update form creates a new post instead of updating the existing one,
what am I missing in here? can you help me?
Hi Tarek,
We’ve sent you a reply via email so we can request more information in other to assist you.
Best,
Don’t know if it’s just me, or something I’ve done, but I can’t for the life of me get the post category to populate its value.
https://imgur.com/a/OyH1ivE
Above is the setup for the checkboxes field, it’s populating the categories correctly but not populating the current post’s category
Hi Scott,
To get the category for the current post, you have to populate the field from the Post Type and set the filter to check if the Post ID is the Current Post ID. You will then select the category under the Post Meta section of the Value Template, as the value you want to get. If you’re still unable to get this to work, you can get in touch with us via our support form if you have an active Gravity Perks License, so we can look into this further.
Best,
I had the same problem, the solution is to run the snippets before :)
Hi there,
nice work !
Is there any way to conditionally run the new GW_Update_Posts based on form field values ?
I’ve tried to nest it in a function linked to gform_pre_submission, or to gform_after_submission, but even like that it do not trigg ?!
Based on the code below, the idea would be to have an if/else within my function update_gf20_post_content
add_action( ‘gform_pre_submission_20’, ‘update_gf20_post_content’, 10, 2 ); function update_gf20_post_content( $entry, $form ) {
}
Thank’s in advance for your support
Hi jj,
We don’t currently support Conditional Logic with this snippet. There’s a free plugin in the WordPress repo that could work for this use case: https://wordpress.org/plugins/post-update-addon-gravity-forms/.
If you have an active Gravity Perks License, you can get in touch with us via our support form with your account email address and weโll be happy to dig into this further.
Best,
Hi, I have implemented this code and everything works well except one thing: when I enable the code, my post tags are no longer being dynamically populated as they were before. Any ideas as to why this might be?
I would typically dynamically copy the post tags like so: Type: Post Filters: Post ID is Current Post ID Ordering: Post ID Ascending Value: Tag
Thank you.
Hi Kai,
I am not really sure why the tag values aren’t populating, but my guess is that your form setup may not be saving the correct tag value when the posts are updated. If you have an active Gravity Perks License, you can get in touch with us via our support form so we can take a closer look at your form and assist you further.
Best
Has any of this been tested within GravityView – updating fields from within the Edit Entry screen within a View?
Hi Scot,
This is currently not supported. I’ll pass this to our developers as a feature request.
Best,
As soon as such a feature appears, I would love to use your addon! It would be very helpful!
I have found a workaround to this.
I’m using the Advanced Post Creation add-on in my Gravity form (along with Gravity View)
I have added a hidden form field called “Form Post ID” and am using this function to populate it upon initial submission:
add_action( ‘gform_advancedpostcreation_post_after_creation_2’, ‘add_post_id_to_form_entry_meta’, 10, 4 ); add_post_id_to_form_entry_meta( $post_id, $feed, $entry, $form ) {
$values = $post_id; $field = GFAPI::update_entry_field( $entry_id, $input_id, $values );
// adds the post id to form_entry_meta so you grab post_id when updating through GravityView gform_update_meta( $entry[‘id’], $field_id, $values ); }
Then, I can use this code snippet with a minor modification, so I no longer need to create a form to edit posts, I can just use the Gravity View editing screen:
new GW_Update_Posts( array( ‘form_id’ => 2, ‘post_id’ => 103, //This is the id of my hidden Post ID field from above ‘title’ => 125,
) );
I have found a workaround to this.
Iโm using the Advanced Post Creation add-on in my Gravity form (along with Gravity View)
I have added a hidden form field called โForm Post IDโ and am using this function to populate it upon initial submission:
http://snippi.com/s/t8ob0pv
I have not got it working yet with dropdown fields or custom taxonomies, but most fields are updating the post when I update the form in GravityView
This script is great and just what we needed, thank you. One question – on submitting the update form, it seems to be ignoring the custom Confirmation we’ve set for the Gravity Form – either an on-screen message or a standalone page – and instead displays the default “Thanks for contacting us! We will get in touch with you shortly.” message. Is there a way we can override that?
Hi Trevor,
We already followed up on this via email, but I wanted to post this in the comment thread so other customers could see it.
If Akismet marks a GF entry as spam, it will stop notifications and any other feeds attached to the form. If this is happening often on your form, we recommend using other anti-spam methods, such as the Zero Spam plugin or the built-in honeypot feature.
Hi there,
Thank’s for this sniplet !
is there any way to update ACF file field within a repeater or a flexible content field ?
Could this be achieved using your nested form field for instance ?
Any guidance would be greatly appreciated !
Hi,
We have a snippet you can use to map Nested Form entries to ACF fields within a Repeater to create the post, but I am not sure it will work for updating the post fields. If you have an active Advanced or Pro Gravity Forms license, you can get in touch with us via our support form with your account email address, so we can get our developers to take a look at this and see if it’s possible to add support for what you want.
Best,
how cool, i’ll probably give it a try ! thank’s
How can we map the address fields subfields to the post’s custom fields?
Hi Andrew,
Each subfield of an Address field has a modifier appended to the Field ID. So, to map the country subfield of an address field with ID 10, you will have something like this,
'custom_field' => '10.6'
.Here is the full list of the address field modifiers.
I hope this helps.
Best,
Thats nice updates with post status and featured image How about the ‘comment_status’ and ‘slug’? I can not find any way to change them Thanks
Hi Alpha,
The snippet should allow you to update post and page custom fields.
About the comment_status and slug, this looks like a feature that will require some digging. As I mentioned earlier if you have an active Gravity Perks License, can you open a ticket with us via our support form, with your account email so that our developers can take a look into it.
Best,
Hi,
We’ve added support to update the Post slug. To update the slug, you’ll pass the ID of the field with the slug as a parameter like so;
new GW_Update_Posts( array( 'form_id' => 123, 'post_id' => 1, 'slug' => 2
));โI hope this helps.
So far I’ve been able to edit existing posts using this snippet with two exceptions.
[1] I can’t seem to expose the custom excerpt value of the existing post. These posts have one explicitly set.
[2] I’m using the new File Upload Pro that is linked to an ACF field on the form that creates the post. I can’t work out how to make this field editable on this form. The File Upload Pro doesn’t have an populatable fields.
Hi Damian,
1) When you say expose, are you implying you can’t populate a field with the custom excerpt value? What exactly is the issue here, are you unable to find the post meta for the custom excerpt field or it doesn’t populate the value even if you select it. We’ll have to take a closer look at the form set up to determine why it isn’t working, so if you have a Gravity Perks License, please get in touch with us via the support form so we assist you further.
2) Populating a File Upload field is currently not possible, but I’m guessing if your intention is to be able to update the ACF field with the uploaded image, you should be able to do that by setting up the snippet parameter to map the File upload field to the ACF field.
Best
Thanks for the reply Samuel.
For [1] it’s not a custom excerpt, it’s the post’s own excerpt.
Using a gravity for we are creating a post and that includes the use of an Excerpt field. But now this new form to edit the post does not have the ability to pre-populate with the saved excerpt.
https://ibb.co/JpC5ZxQ
For [2] I understand now. If we make the file upload optional on the editing form (it’s required on the creation form) then the user can optionally replace it – we will map it accordingly, or if they leave it blank the existing upload remains. Thanks for the clarification.
Hi Damian,
You can set the Value Template to Custom Value and enter {post:post_excerpt} to get the Post Excerpt.
Best,
Great! Thank you.
Is there somewhere in the docs online that I could have found this? I like to be self sufficient where I can be.
Hi Damian,
If you have a Gravity Perks license, you can get in touch with us via our support form and weโll be happy to dig into this further.
Best,
Hi Dario,
I do have a license and will submit a support request now as the excerpt part is working but a new post is being created instead of updating an existing one.
i simply created a post feed and populated the post id and then created a feed with pods to update post – this works when form is on the post page nd so the user can click modal pop up and fill out form and the form refreshes – or reloads page upon form submission and post is updated and shows new fields immediately – i’m not sure what this snippet does that is not already doable without it?
does this allow my to add the form to non post page and the user select the post they ant to edit and it update from say the form submission without having to be on post page?
Hi Nate,
Exactly. So using the snippet together with GP Populate Anything, you can populate a Dropdown field with the list of posts, and based on the selection, all other fields(Post ID, Post Content, custom fields, etc) will be populated with respective data. With this setup, when the form is submitted it will update the selected Post and this works even without the form being on a Post Page.
Best
A great additional would be to change the post status on submission as well (i.e. draft, published etc).
Hi David,
I’ll pass your feature request to our developers. If and when this is added, we’ll let you know by replying to this comment or send you an email.
Best,
We’ve added support to updating the post status on submission.
I’m using this setup to allow game designers to post and then have edit access to games they are listing as available to play at our online playtesting convention.
I would like to also have a field in the custom post type for games called “Interested Players” that displays the names of people who have expressed interest. I’m thinking I can have people submit a separate very short form in which they select the game in question from a dropdown created by Populate Anything and share their name as an interested player.
I’m still working on setting up my forms to populate and edit the game posts, but my understanding is that whatever is submitted from the form will completely overwrite whatever was in the post’s field before.
I think it would technically work to use Populate Anything to show the current list of interested players and ask the user to add their name to the end. Unfortunately, there are a lot of users who can’t/won’t read instructions, so I feel I’d need a more errorproof method.
Is there a way to set this up so it will append whatever someone enters to the end of what’s already there?
Thanks!
Hi Heather,
A solution that would work for you with the snippet will be to use Live Merge tags to populate a hidden field that will be used to update the Custom field. What you’ll do here is to first populate a hidden field with all the names that have already been stored in the Custom field. Then you’ll use the Live Merge of this first hidden field, to set the default value of another hidden field. You will also add the Live Merge tag of the field the user will enter their name to the second hidden field. With this setup, the ID second hidden field is what will be used to configure the snippet to update the Post’s Custom field. I hope this helps. In case you’ll need further assistance or have any questions, you can get in touch via our support form.
Best,
Hi Scott, You mention the Advanced Post Creation plug-in but it’s not part of your steps. Can you clarify whether this method is intended to work with APC? My use case is for Subscribers who don’t have elevated privileges to create posts. It seems this requires APC, but when I activate that in the context of this solution, new posts are created instead of re-writing the existing posts. Turn APC off and the existing posts are re-written but ordinary Subscribers can’t write.
Thanks
Hi Dustin,
The Update Post snippet doesn’t require the Advanced Post Creation add-on to work. The Advanced Post Creation add-on can only create new posts but doesn’t update existing posts. So if you’re to use both the Advanced Post Creation addon together with the snippet, a new post will be created and at the same, a matching existing post will be updated when the form is submitted. If you would want to use the same form for creating a new post and also for updating existing posts, then you’ll have to set up a conditional logic on the Advanced Post Creation feed using the Post ID field, such that the feed isn’t executed if there is a valid Post ID in the field.
Also, for security reasons, the snippet will only update posts that the current user has capabilities to update, so I am not really sure if an ordinary subscriber as you have it can be allowed to update a post without some customization to the snippet. In case your ordinary subscribers do not have the capability to update but you want them to be able to update existing posts, then you can comment out or delete this part below from the Update Post snippet.
if ( ! current_user_can( 'edit_post', $post->ID ) ) { return; }
Best,
This is great!
What steps could I take to use this to update custom taxonomy of a post, if that is possible?
Hi Leela,
Currently, the snippet doesn’t support updating custom taxonomy and it will require some customization to get it to do that. I’m forwarding this functionality to our developers as a feature request for the snippet.
Best,
We create a bunch of “events” using The Events Calendar Pro plugin and then we want to use Gravity forms for people to sign up as the “Organizer” for the event and then allow them to use the Community functionality of The Events Calendar to moderate RSVPs, Venues, etc.
To do this, we just need to update the Author and Organizer fields in that event. We do not need to update title and content or any other fields at this time. Can this snippet be modified to edit just those 2 fields?
Ideally, the user would be able to select multiple events from a checkbox list in the form and update all of those “posts” if possible.
Hi, I’m having trouble getting this form to update custom fields. It updates title and content no problem, but not custom fields. Not sure what I’m doing wrong.
If I manually enter the value in the WP admin, Populate Anything will show the value in the form. But if I enter a new value, it won’t update.
Here’s the snippet I’m using: http://snippi.com/s/99trj99
Solved it. I was testing the form with a new user account that only had Subscriber privileges. Once I elevated the user account to Author, everything worked fine. This would probably be worth adding to your troubleshooting page.
Hi Hannah,
Thanks for the update and glad to know you were able to figure out what was wrong.
Best,
Nice one! If you had additional fields in the form, say for a job listing, would you be able to map those fields as well if for example they were existing taxonomies for a CPT?
Hi Scott,
We’ve updated the snippet to support mapping custom fields. Could you give the updated snippet a try with this version snippet usage;
new GW_Update_Posts( array( 'form_id' => 123, 'post_id' => 1, 'title' => 2, 'content' => 3, 'meta' => array( 'custom_field' => 4, 'another_custom_field' => 5 ) ) );
Best,
Hi! Amazing, thats so useful! Can get meta fields updated, too? Best Tom
Hi Thomas,
We’ve updated the snippet to support mapping custom fields. I’m guessing this should work for you also. The snippet usage will be as follows;
new GW_Update_Posts( array( 'form_id' => 123, 'post_id' => 1, 'title' => 2, 'content' => 3, 'meta' => array( 'custom_field' => 4, 'another_custom_field' => 5 ) ) );
I hope this helps.