Set Post Status by Field Value
Currently, the post status of a post generated from a Gravity Form submission is set via the Post Title, Post Body or Post Excerpt fields. So what do you do if you want the post status to be based on some form of user input? It’s a form-specific option (as opposed to field specific) which means Gravity Forms’ powerful conditional logic can not help us here.
Fortunately, the solution is quite simple. Put on your pointy wizard hats and check out this snippet.
How do I install this snippet?
Use a simple copy and paste spell to copy the snippet above and paste it in your theme’s functions.php file.
Do I need to modify the snippet to work for my form?
Yes. There are comments inline in the code which indicate where you’ll need to make modifications.
- Update the
123
in the filter namegform_post_data_123
to the ID of your own form. - Update the
4
in both places you see$entry[4]
to your field ID.
This snippet assumes the value of the specified field will be a valid post status. Here is an example of how this can be configured using a drop down field:
FAQs
Is it possible to set the post status based on the option label or a value that is not a standard post status?
Is Gravity Forms made of magic? Yes and yes! The snippet to accomplish this is only slightly more involved. I’ve added this as a separate post here: Set Post Status by Field Value (Advanced)
Summary
This is the simplest method to accomplish this functionality. Make sure you check out the advanced version too!
Thanks for the post, David.
Would it be possible to create three separate Gravity forms with the same function?
I wrote the same snippet three times on function.php, and replaced the form ID (gform_post_data_n) and field ID ($entry[n]) in each to correspond to the respective Gravity form. A refresh on the website returns a blank white page.
Hi Josh, that should work. I’m guessing there may have been a syntax error. This might help: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Hi David,
I’m having the same problem as Josh. Trying to set a post status by two different forms results in a blank page. Could you please explain how the quoted article (https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/) works in connection with the snippet above?
Thanks in advance!
Hi Eckart, this is a bit simpler than a class-based snippet. You just need to have unique function names. Here’s an example: http://pastie.org/private/ppfgtq1f026dcg9ccgkag#3-4,13-14