PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Update a custom field based on a form response

  1. I'm including a form within the template for an existing custom post type on a non-profit site. The custom post type will have a custom field for sponsors with a value of 0-5, and I want to both get that value for display in the form, as well as update the field when the form is submitted.

    For instance, a visitor to the site might see that there is 1 sponsor slot already taken, and they want to offer two more. I'd like the form to:

    1) Query the field to properly display how many sponsorships are available. (In this case, only 4, so it would display a drop down with 1-4.)
    2) Assuming the user selects the number 2, for two sponsorships, the form would the update the custom field to add 2 to it, so that it would now display that 3 sponsorships have been taken for the next user.
    3) To possibly confuse the issue, the number of sponsorships should be taken from a product quantity as it will also be processing their payment.

    Is this doable? And if so, any pointers on the proper code?

    Posted 12 years ago on Wednesday September 26, 2012 | Permalink
  2. I think I've worked out a solution for part what is described here, but still haven't resolved one key feature. When I form is submitted, I'd also like it to update a custom field for the current post that the form is displayed within. Is this doable?

    Posted 12 years ago on Friday September 28, 2012 | Permalink
  3. If you want to update an existing post (the post where the form is embedded) you can use the gform_after_submission hook and call update_post_meta to add or update the custom field value.

    http://www.gravityhelp.com/documentation/page/Gform_after_submission
    http://codex.wordpress.org/Function_Reference/update_post_meta

    Posted 12 years ago on Monday October 1, 2012 | Permalink
  4. Thanks Chris

    Posted 12 years ago on Monday October 1, 2012 | Permalink
  5. Did you get it working or did you need additional assistance?

    Posted 12 years ago on Monday October 1, 2012 | Permalink
  6. Not working quite yet. Here's what I have:

    <?php add_action("gform_after_submission_9", "update_sponsor_count", 10, 2);
    			function update_sponsor_count($entry, $form){
    			    //getting post and entry data
    			    $post = get_post($entry["post_id"]);
    			    $sponsor_level = get_post($entry["3"]);
    			    //updating sponsor count
    			    if ($sponsor_level == 'sponsor1') { $new_count = $sponsor_count + 1;}
    			    update_post_meta($post, 'sponsors', $new_count);
    			} ?>

    I'm trying to update the 'sponsor' post meta. The $sponsor_count is previously defined in the post loop as the value of 'sponsor' prior to form submission. Not showing any php errors in the error log.

    Posted 12 years ago on Monday October 1, 2012 | Permalink
  7. Can you echo the variables after you set each one to see if they are all populated as expected?

    However, I think the problem is in line 8. $post is an array. You need the post ID for update_post_meta. Something like this instead:

    [php]
    update_post_meta($post->ID, 'sponsors', $new_count);
    Posted 12 years ago on Monday October 1, 2012 | Permalink
  8. Thanks. I've almost got it worked out, except for one last issue with an Entry Object. Field 3 is a Product Drop-Down, so using $sponsor_level = $entry["3"] is returning both the Value and the Price with a pipe between them. I've tried "3,1" but that doesn't work. Is it possible to return only the value field from that dropdown?

    Posted 12 years ago on Monday October 1, 2012 | Permalink
  9. I think you are going to have to split that string on the pipe and save the first part, the value, if that's what you want. If you are doing this:

    [php]
    $sponsor_level = $entry["3"] ;

    try this instead:

    [php]
    $sponsor_level_array = preg_split("/\|/", $entry['3']);
    $sponsor_level = $sponsor_level_array[0];

    That will return the first part of the entry, before the pipe, the value.

    There is probably a shorter way to do that but I can't put my finger on it right now.

    Posted 12 years ago on Tuesday October 2, 2012 | Permalink