Home › Forums › General Issues › Automatically fill image field with post_thumbnail › Reply To: Automatically fill image field with post_thumbnail
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');
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
π Weβre excited to announce we've released Composer support for installing ACF PRO.
— Advanced Custom Fields (@wp_acf) January 31, 2023
π #ComposerPHP fans rejoice!
β¨ Please see the release post for all the details and full instructions. https://t.co/ebEfp61nlR
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.