Home Forums General Issues Automatically fill image field with post_thumbnail


Automatically fill image field with post_thumbnail

  • Hi there,

    I am using Gravity forms to create posts on the frontend and currently use the featured image option to create a post_thumbnail for frontend submissions. Is there a way that ACF could automatically fill an image field from a post’s post_thumbnail, e.g. “if has post thumbnail, then fill custom field with post thumbnail id”?

    All the best & thanks a lot in advance


  • Just after the post is saved

    function add_image_to_acf_field($post_id) {
      if (has_post_thumbnail($post_id)) {
        $post_thumbnail_id = get_post_thumbnail_id($post_id);
        // notice that I'm using the field key here
        update_field('field_4fc5ab37e1819lol', $post_thumbnail_id, $post_id');
    add_action('save_post', 'add_image_to_acf_field');
  • Hi Hube2,

    I tried it out, tied it to the right field key (the one I found with the ‘_field_name’ in the database), but it still does not fill the image field unfortunately. What format should I choose when creating the field? It’s ID at the moment….

    All the best


    Edit: Just to make sure, I put this into my functions.php, right?

    Edit 2: There is a small typo, no ‘ needed after $post_id in line 5. Still not working :D.

  • Yes, you could put this in functions.php

    The format shouldn’t matter. ACF stores the attachment ID as the field value no matter what you set it up do return.

    Not sure, it looks like it should work. I will see if I can find some time to test it. Basically we car calling our function whenever the post is updated, grabbing the featured image id and stuffing it into the ACF field which is kinda why I’m confused as to why it doesn’t work (even though I didn’t test it before I posted it)

  • Mh, maybe it is something how Gravity forms fills out the post_thumbnail field and/or saves posts and custom fields? I managed to fill other (text-based) fields with Gravity forms frontend posting, but so far the image field fails (had a similar problem with a date picker field – even though it had the same format, it never reached the actual ACF field when saving a post with Gravity forms). So maybe the complication is somewhere along GF’s creation of posts.

  • I just tested it out and it worked for me. The key of the field you should be able to see when editing the field group.

    Check Screen Options -> Show Field Keys

    The only thing I can think of is that the field key is wrong…

    Or that update field is not working because it’s on the front end.

    Is this image field part of something more complex, like a repeater or flexible content? If not you could try the following:

      function add_image_to_acf_field($post_id) {
        if (has_post_thumbnail($post_id)) {
          $post_thumbnail_id = get_post_thumbnail_id($post_id);
          $image_field_name = 'image_field';
          $image_field_key = 'field_54ea66db22c08';
          update_post_meta($post_id, $image_field_name, $post_thumbnail_id);
          update_post_meta($post_id, '_'.$image_field_name, $image_field_key);
      add_action('save_post', 'add_image_to_acf_field');
  • Oh, I may have come across the problem itself.

    add_action('save_post', 'add_image_to_acf_field');

    is supposed to trigger when a post is published, right, not when a post is saved regardless of the saving type?

    The front-end submissions only get saved as drafts, and once I publish it afterwards (i.e. open in backend and publish it), the field gets filled properly. So the draft state might be the problem?

  • According to the documentation the hook should be fired whenever a post or page is created or updated. It should fire even if the post is saved as a draft, I think.

  • Oh well, then this might just be something on Gravity form’s end I suppose, since I had trouble filling other (non-text) ACF fields as well. Too bad, but thank you for your help anyway! There doesn’t seem to be a different hook for gravity forms submissions to alter a field before saving it..

  • You might want to check with Gravity Forms. Maybe they bypass the usual post insertion process in WP and that’s why the save_post hook is not firing. I’ve never used GF, but I’d like to know what you find out. If GF bypasses standard WP operations then it would just be a reason for me to not use it.

  • Hey Hube2,

    I would really like to, but the project I am working on is strictly on a limited budget on which I already toss in extra hours. This functionality is actually a personal plus, and since I have such a workload I probably won’t have the time to handle Gravity form’s specialities this time around. If I find another project where I need it again I will investigate more. Thank you so far for your effort in helping out on this matter. It worked, in the backend, flawlessly, too bad not with GF at all :/.



Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Automatically fill image field with post_thumbnail’ is closed to new replies.