Home › Forums › Backend Issues (wp-admin) › Set a custom field as featured image caption › Reply To: Set a custom field as featured image caption
Support pointed out that there is a typo, but this worked perfectly for me. Fixed typo below:
add_filter('acf/save_post', 'update_featured_image_caption', 20, 1);
function update_featured_image_caption($post_id) {
// get the featured image id of the post
$id = get_post_thumbnail_id();
// if not set, exit
if (!$id) {
return;
}
// get the field you want to use for the image captions
$excerpt = get_field('your-field-name', $post_id);
// if not set, exit
if (!$excerpt) {
return;
}
// get the attachment post
$attachment = get_post($id);
// alter the caption (post_excerpt)
$attachment->post_excerpt = $excerpt;
// disable this filter to prevent infinite loop
remove_filter('acf/save_post', 'update_featured_image_caption', 20);
// update the attachment post
wp_update_post($attachment);
// re-enable this filter
add_filter('acf/save_post', 'update_featured_image_caption', 20, 1);
}
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.