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.

gform_post_data - Updating a post

  1. Hey guys,

    I'm doing a bunch of custom stuff with my forms and need a little help figuring out one last step.

    Here's a bit of background:
    - I'm using my form to create a custom post
    - When the custom post is saved/published, a row is inserted into a custom table
    - I need my second form (an update form) to update this custom post
    - I'm using gform_post_data_2 to target my update form

    I'm trying to figure out how to update the featured image of the post. I can't find any functions that will do it for me.

    Anyone have any tips?

    Thanks,
    Jacob

    Posted 12 years ago on Monday September 26, 2011 | Permalink
  2. Did you ever figure this out? I'm looking for the same thing.

    Posted 11 years ago on Tuesday August 7, 2012 | Permalink
  3. @parkeast, please ask your question here and explain what you would like to do, and what you've already tried, and we can help address your issue specifically. Thank you.

    Posted 11 years ago on Tuesday August 7, 2012 | Permalink
  4. I have a form that let's people submit an image to a post which is already set. The form uses the gform_after_submission and update_post_meta to update the fileds on the form, but I'm not sure how to have it set the featured image. It uploads the image just fine... but having the "set as featured image" box checked on the upload box in the admin side doesn't effect the function because it's editing, not creating, a post.

    Posted 11 years ago on Tuesday August 7, 2012 | Permalink
  5. Do you want to set the most recently uploaded image as the featured image then? You probably want to use set_post_thumbnail in the function you hooked to gform_after_submission.

    http://codex.wordpress.org/Function_Reference/set_post_thumbnail

    Grab the last attachment to the post, and set that as the post thumbnail. Can you try that?

    Posted 11 years ago on Tuesday August 7, 2012 | Permalink
  6. Thanks!
    I'll try it and let you know...

    Posted 11 years ago on Wednesday August 8, 2012 | Permalink
  7. Please let us know how it goes. Thank you.

    Posted 11 years ago on Wednesday August 8, 2012 | Permalink
  8. Okay... here's what I'm trying now... to no avail.

    add_action( 'gform_after_submission_6', 'gf_edit_student', 10, 2 );
    function gf_edit_student( $entry, $form ) {
    global $post;
    global $wpdb;
    global $wp_query;
    $postID = $wp_query->post->ID;
    
    $post_thumbnail = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    
    $post_thumbnailID = wp_get_attachment_metadata($post_thumbnail);
    
    set_post_thumbnail($postID, $post_thumbnailID);
    }
    Posted 11 years ago on Wednesday August 8, 2012 | Permalink
  9. GOT IT!

    // update the "12" to the ID of your form
    add_filter("gform_post_submission_12", "gform_set_post_thumbnail");
    function gform_set_post_thumbnail($entry){
    
        // get the last image added to the post
        $attachments = get_posts(array('numberposts' => '1', 'post_parent' => $postID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC'));
    
        if(sizeof($attachments) == 0)
            return; //no images attached to the post
    
        // set image as the post thumbnail
        set_post_thumbnail($postID, $attachments[0]->ID);
    }
    Posted 11 years ago on Thursday August 16, 2012 | Permalink
  10. Looks good. Thank you for the update.

    Posted 11 years ago on Friday August 17, 2012 | Permalink