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.

Dynamically populate custom field with attachment id created by post image field

  1. Hello,

    First things first - Gravity Forms is amazing. Taking a look at the code under the hood... Me Gusta!

    And on to my issue...

    I am trying to populate a custom field's value with the attachment id generated by a separate post image field. I'm figuring I'll need to do something with functions fired on the gform_post_submission or gform_post_data hooks, but I'm really not sure how to get at the attachment ids properly.

    Here is what I have now. It sort of gets the job done, but it's also pretty fragile due to the way that I'm determining the attachment IDs.

    <?php
    add_action( "gform_post_submission", "post_submission_handler" );
    function post_submission_handler( $entry ){
    
      // Hacky way of determining attachment id
      #update_post_meta( $entry['post_id'], 'my_meta_key', $entry['post_id'] + 1); 
    
      // Less hacky, but still hacky way of determining the attachment id.
      $meta_key = 'my_meta_key';
      $attachment_index = 0; // 0 indexed. For example, GF post image field 1 will be the first attachment.
      $args = array(
        'order'          => 'ASC',
        'post_type'      => 'attachment',
        'post_parent'    => $entry['post_id'],
        'post_mime_type' => 'image',
        'post_status'    => null,
        'numberposts'    => -1,
      );
      $attachments = get_posts( $args );
    
      if ( $attachments ) {
        $counter = 0;
        foreach ( $attachments as $attachment ) {
          if ( $counter == $attachment_index ) {
            update_post_meta( $entry['post_id'], $meta_key, $attachment->ID );
            break;
          }
        }
      }
    
      return $entry;
    }

    I also toyed around with this idea, but it won't work because the GF image url is not the same as a post guid (not that this is a good approach to begin with, but at this point, I'm trying to just make it work.)

    <?php
    add_filter("gform_post_data", "gfcustom_set_pattern_post_type", 10, 2);
    function gfcustom_set_pattern_post_type($post_data, $form){
    
      global $wpdb;
      $image_url = $post_data['images'][0]['url']; // this is not a guid though... (not to mention the 0 index approach being weak)
    
      // ...so this will not work:
      $thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE guid = '$image_url'" ) );
      $post_data['post_custom_fields']['my_custom_field'] = $thepost->ID;
    
      return $post_data;
    }

    -goto10 (via wilson)

    Posted 13 years ago on Sunday February 6, 2011 | Permalink
  2. Not sure if it helps, but I'm running 1.5.RC3.5. Also, maybe I was a little too verbose the first time around.

    In short, how can I reliably grab the id of the attached image generated by a particular post image field when the form is processed?

    -goto10 (via wilson)

    Posted 13 years ago on Tuesday February 8, 2011 | Permalink
  3. Hi Wilrevehl,

    Since the post these images are attached to has just been created, there will be no other attachments to this post besides the image being uploaded in conjunction with the form submission. Because of this, it would be safe to use the method where you retrieve all attachments of the post that was just created. Since there will only be one attachment it is also safe to use the '0' index to retrieve the properties of that attachment. Here is the code I used to complete this:

    http://pastie.org/1541593

    Posted 13 years ago on Tuesday February 8, 2011 | Permalink
  4. Thanks for your confirmation, David. Yes, that will get the job done since I only have 1 image, but I have designed my custom post types to handle many images, and things will get tricky fast when I need to leverage that. (I'm thinking I might be able to utilize some kind of manual index relationship, but still, not ideal).

    Is there any chance that a mechanism could be put in place that would help us to maintain an association between the post image field and the uploaded item?

    I think it would be cool if an additional field (I know...) could be added to the post image form control where a custom field name could be entered. This custom field would be automatically updated when the attachment was made. Likewise, after a form has already been populated, the custom field could be referenced to look up the image that was already uploaded for the post image field that it is associated with.

    I realize that this is a bit different from how the post image field works currently, but I think that this functionality would complement the existing control nicely. ...I also know that dealing with images in this way has its own idiosyncrasies. Like when an attachment is deleted directly from the media library, rendering the attachment id stored in the custom field orphaned. I dunno, just putting it out there.

    Cheers

    -goto10 via Wilson (I work for Wilson btw. :) )

    Posted 13 years ago on Tuesday February 8, 2011 | Permalink
  5. liamk23
    Member

    Agreed! This would be a really nice feature. In the mean time can the code David Smith posted be used to retrieve the attachment URL of the first 'post image' field regardless of how many 'post image' fields are used in the same form?

    All i wish to do is set the featured image of a post with the first image uploaded on a form via the 'post image' field.

    Thanks for any help.

    Posted 13 years ago on Saturday April 2, 2011 | Permalink
  6. liamk23
    Member

    This solution seems to work with no problems...

    http://www.gravityhelp.com/forums/topic/auto-setting-the-the_post_thumbnail

    Posted 13 years ago on Saturday April 2, 2011 | Permalink