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.

Auto setting the "the_post_thumbnail"

  1. drewbaker
    Member

    Hey Guys,
    Does anyone know how to have the "the_post_thumbnail" set when a post is submitted via a form? I'm happy for it to just be the first image attached.

    Alex The Admin said it was possible in a pre-sale post here: http://forum.gravityhelp.com/topic/multiple-image-attachment

    Thanks for the help! Great plugin!

    PS: You guys should have some sort of user-edited code snippet repository along the lines of the wordpress CODEX for this kind of thing. Would save your admins a lot of work on the forums.

    Posted 12 years ago on Monday May 10, 2010 | Permalink
  2. EDIT (July 2011)

    PLEASE NOTE: This code is out of date and no longer works.

    Please see this post instead:
    http://www.gravityhelp.com/forums/topic/fatal-error-when-using-the_post_thumbnail-code-snippet#post-30918

    I like your idea about having some sort of collaboration on these code snippets. Could work really well.

    Try the following code snippet to set your post thumbnails

    Ignore the code below

    add_filter("gform_post_submission", "set_post_thumbnail", 10, 2);
    function set_post_thumbnail($entry, $form){

    //Replace 4 with your actual form id
    if($form["id"] != 4)
    return;

    //getting first image associated with the post
    $post_id = $entry["post_id"];
    $attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));
    if(sizeof($attachments) == 0)
    return; //no images attached to the post

    //setting first image as the thumbnail for this post
    update_post_meta( $post_id, '_thumbnail_id', $attachments[0]->ID);

    }

    EDIT (July 2011)
    See this post for an updated solution:
    http://www.gravityhelp.com/forums/topic/fatal-error-when-using-the_post_thumbnail-code-snippet#post-30918

    Posted 12 years ago on Tuesday May 11, 2010 | Permalink
  3. Great! thank you so much, it works perfectly.

    Posted 11 years ago on Sunday November 14, 2010 | Permalink
  4. Hi, what file should the above code be added to?

    Posted 11 years ago on Tuesday January 25, 2011 | Permalink
  5. Hi webrulon, that should be added to your functions.php

    Posted 11 years ago on Tuesday January 25, 2011 | Permalink
  6. Thanks Alex. This is exactly what I was looking for. It's a bit too well hidden though, especially considering how many people are asking for this particular function. Maybe make this thread a sticky in the forum so people find it easily?

    Posted 11 years ago on Tuesday February 8, 2011 | Permalink
  7. Thank you so much! Perfect for what I need.

    Posted 11 years ago on Saturday February 12, 2011 | Permalink

This topic has been resolved and has been closed to new replies.