Support

Account

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);
    }