Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

  • Gravity Perks
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    • Documentation
    • Support
    • Account

Set Post Status by Field Value (Advanced)

Last updated August 21, 2019 | Written by David Smith 16 Comments

This tutorial/snippet expands on the Set Post Status by Field Value snippet. Here is a refresher on the issue:

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.

The original snippet demonstrated the simplest approach and relied on the field values being set as valid post statuses. This snippet allows you to format the field values/labels however you want and then map which field values correspond to which post statuses in the code.

Download Code
/**
* Set Post Status by Field Value (Advanced)
* http://gravitywiz.com/2012/05/04/set-post-status-by-field-value-advanced/
*/
// update "3" to the ID of your form
add_filter('gform_post_data_3', 'gform_dynamic_post_status', 10, 3);
function gform_dynamic_post_status($post_data, $form, $entry) {
// update "5" to the ID of your custom post status field
if($entry[5]) {
switch($entry[5]) {
case 'Yes, please review my post.':
$post_data['post_status'] = 'pending';
break;
case 'No, please publish my post.':
$post_data['post_status'] = 'publish';
break;
}
}
return $post_data;
}
view raw gistfile1.php hosted with ❤ by GitHub

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. Make note of the comments inline for pointers on where you will need to make your modifications.

  1. Update the 3 in the filter name gform_post_data_3 to the ID of your own form.
  2. Update the 5 in the two places you see $entry[5] to your field ID.
  3. Update each case statement (ie case 'Yes, please review my post.':) to match one of your field value options.
  4. Update each $post_data['post_status'] = 'your_post_status' assignment to equal the correct post status for the corresponding field value.

With this snippet you can map any field value to any post status. This snippet would work with a field configured like so:

Set Post Status by Field Value (Advanced)

Even More Advanced

At the risk of exhausting this topic (wizards like to be thorough!), I want to share one more example of setting the post status by field value: setting the post status by a Product field value.

/**
* Set Post Status by Product Field Value
* http://gravitywiz.com/2012/05/04/set-post-status-by-field-value-advanced/
*/
// update "3" to the ID of your form
add_filter('gform_post_data_3', 'gform_dynamic_post_status', 10, 3);
function gform_dynamic_post_status($post_data, $form, $entry) {
// update "6" to the ID of your custom post status field
if($entry[6]) {
$values = explode('|', $entry[6]);
switch($values[0]) {
case 'Basic Package':
$post_data['post_status'] = 'draft';
break;
case 'Premium Package':
$post_data['post_status'] = 'publish';
break;
}
}
return $post_data;
}
view raw gistfile1.php hosted with ❤ by GitHub

Usage

You can follow the same installation and modification instructions above. The only difference is this little line of code.

$values = explode('|', $entry[6]);

A product field value is stored as a pipe (|) delimited list of values. The first value will be the product label or, if values are available and enabled for the Product field type (ie Drop Down Product Fields), the label will be replaced by the value as the first value. The second value will be the price.

Format template$entry[6] = 'label/value|price'
Format example w/ values enabled$entry[6] = 'basic|0'
Format example w/ values not enabled$entry[6] = 'Basic Package|0'

For this example, we just want the label. We use the PHP explode() function to “split” the combined string into separate pieces where ever there is a pipe. This gives us an array of $values.

Before Explode$values = 'Basic Package|0'
After Explode
$values = Array
(
    [0] => 'Basic Package'
    [1] => 0
)

We know that the product option label will be the first value in our $values array because it was the first value in the combined product value string. An array’s index starts at 0 so we can assume that $values[0] will give us the label of the selected product option.

With that in mind, we can create a switch case (just like we did in the previous snippet) and set the post status dynamically based on which product option was selected.

switch($values[0]) {
case 'Basic Package':
$post_data['post_status'] = 'draft';
break;
case 'Premium Package':
$post_data['post_status'] = 'publish';
break;
}
view raw gistfile1.php hosted with ❤ by GitHub

Here is an example of how the Product field might be configured:

Set Post Status by Product Field Value

Summary

Whew! That’s a lot to take in. I think I’m going to go have a rest in my tower. If you have any questions, just ask!

Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 32+ essential add-ons for Gravity Forms with support you can count on.

  • View All Perks
  • Buy Gravity Perks

Filed Under: Snippets, Tutorials

Comments

  1. Scot says

    October 12, 2018 at 10:02 am

    David

    Will this work with radio or checkbox fields versus dropdowns (I see you use dropdowns in the example).

    Also, if I have 3 different forms, is it ok to drop the three instances of the code into functions for the specific form/field or is there a way to combine it into one?

    Thanks

    Reply
    • David Smith says

      October 12, 2018 at 10:13 am

      Hey Scot, yes, this will work with Radio Button fields. I’m not sure how it would work with Checkbox fields since multiple statuses could be selected? You would need to target each checkbox input individually as well (i.e. $entry[2.3]).

      Given the basic nature of these snippets, I could just create 3 different instances of the snippet. You would need to rename the function for each instance.

      gform_dynamic_post_status1 gform_dynamic_post_status2 gform_dynamic_post_status3

  2. Scot MacDonald says

    May 25, 2017 at 3:41 pm

    Hi

    This post is so close to my own issue I thought I comment here and hope I’m not taking the conversation off-track.

    I’m using GF+CPT with WP Job Manager Resumes and GF User Registration.

    New users complete a registration form and are directed to a profile page where they complete a second form with fields mapped to the resume post type (via Title field). I have the second form setup to update the user’s information via the User Registration Update User feed option, so the data remains persistent in the form and also successfully updates the resume display each time it is submitted.

    The issue is every time the user updates the form it creates a new resume which still displays fine since by design WPJM Resume always displays the latest resume, but I was hoping to find a way to get around the duplicate posts since they will appear in search results.

    Is there some way the Post Status dropdown in the Title field could include a custom “post status” versus “publish”, or perhaps a way to update the post versus publishing a new version each time?

    Thanks!

    Reply
    • David Smith says

      May 25, 2017 at 5:03 pm

      Hi Scot, for updating posts generated by Gravity Forms, I’d check out this plugin: https://wordpress.org/plugins/gravity-forms-post-updates/

  3. corporatic says

    March 5, 2017 at 7:14 am

    Hi,

    How can I set the visibility of the “pending/publish” field only to admins and editors? and for an specific user? I have got it with css but it is an unsecure method…

    Ca you please help?

    Reply
    • David Smith says

      March 5, 2017 at 6:31 pm

      I’d hit Gravity Forms support for general questions like this. They’ve got a great support team ready to help. :)

  4. corporatic says

    March 1, 2017 at 7:28 pm

    HI, great post!!

    I´m using sticky list and I want my forms to have pending status the first time only. When a user edit his own form (after admin accepted it) the state should be published.

    I like sticky list but it is not exactly what I want, do you have a better solution to update a post through GF? I´m willing to pay for something like that.

    Thanks a lot.

    Regards

    Reply
    • David Smith says

      March 1, 2017 at 11:28 pm

      This might be a better solution: https://wordpress.org/plugins/gravity-forms-post-updates/

  5. James says

    January 4, 2014 at 4:46 pm

    Thanks for this code. I was putting a form on a radio station website for churches and schools to submit closings/delays for publication on the website and on the air. The radio station issues a code that must be used so that they don’t get fake submissions.

    I needed to have a field for that code and either publish the post if they had the correct code or place it in “Pending Review” if they either put in the wrong code or they don’t put in any code. This worked perfectly. I just set the default on the form to “Pending Review” and then put one case in the code above that noted the code. It worked like a charm.

    Reply
    • David Smith says

      January 5, 2014 at 11:30 am

      Awesome! Glad to hear this worked for you. :)

  6. Rikard says

    October 31, 2013 at 8:07 am

    Okey, let’s face it. I will stick to your first solution.

    Have a nice day!

    Rikard

    Reply
  7. Rikard says

    October 31, 2013 at 5:54 am

    Exactly was I’m looking for David! A question though, is this working with multisites? I have several sites, all using Gravity forms but only one function file.

    All the best,

    Reply
    • David Smith says

      October 31, 2013 at 7:26 am

      Hi Rikard, I’m assuming that each site on your network has it’s own Gravity form that is being used to generate posts? The simplest method is to create multiple instances of this snippet and wrap each in a blog ID based condition. You can then check which blog ID you are on via the get_current_blog_id() function. It’d look something like this.

    • Rikard says

      October 31, 2013 at 7:44 am

      Thanks David for the tip! Is there any way to only use “one” Gravity form for all my network sites? That would be ideal for me.

      Once again, thanks for your reply.

      Rikard

    • David Smith says

      October 31, 2013 at 8:03 am

      No simple way that I know of. You could iframe a Gravity Form on a child site but as far as running it from the shortcode, it would get a little tricky.

Trackbacks

  1. GravityWiz to Help Get the Most out of Gravity Forms says:
    May 8, 2012 at 7:46 pm

    […] Gravity Form users get the most out of Gravity Forms.Here are just a few of their recent posts: Set Post Status by Field Value (Advanced), Set Post Status by Field Value, Pro Tip: Skip Pages on Multi-Page Forms, and Custom Field […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • How To (64)
  • News (21)
  • Plugins (14)
  • Releases (7)
  • Resource (3)
  • Snippets (58)
  • Tutorials (57)
  • Updates (104)

Recent Posts

  • How to Send a Follow-Up and Pre-Fill Information
  • How to Update Posts with Gravity Forms
  • Gravity Wiz Weekly #104
  • The Complete Guide to Using Gravity Forms With Zapier
  • Gravity Wiz Weekly #103

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2021 · Powered by WordPress · Gravity Wiz LLC

  • Support
  • Affiliates
  • About
  • Sitemap
  • Gravity Perks
    ▼
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    ▼
    • Documentation
    • Support
    • Account