I am using this code to automatically add a featured image to a post or custom post type using an advanced custom field.

function acf_set_featured_image( $value, $post_id, $field  ){

    if($value != ''){
      //Add the value which is the image ID to the _thumbnail_id meta data for the current post
      add_post_meta($post_id, '_thumbnail_id', $value);

    return $value;

// acf/update_value/name={$field_name} - filter for a specific field based on it's name
add_filter('acf/update_value/name=fl_image', 'acf_set_featured_image', 10, 3);

However, the image being added is the full size image which is not ideal.

How could I modify this code, to add a smaller version of the image being uploaded on Advanced Custom fields.

My theme already creates smaller size images and also wordpress automatically creates smaller size images if I am not mistaken.

  • Actually I'm not familiar with ACF before so I'm not sure about it but as far I seen you just doing here passing the attachment ID value. Did you see this one ? wordpress.org/support/topic/featured-image-crop-1/#post-6864703 . Can you tell me why you used add_post_meta() here ? And one more thing I think this type of post (Third party plugin) are off topic here, so you better post this one on stackoverflow.com – Mohammad Limon Sep 26 '16 at 19:41
  • Thanks for the comment. the link you posted is referring to an extension of ACF which I am not using. I was hoping that I could fix the above code using a native wordpress function. – Joanna Mikalai Sep 26 '16 at 22:11
  • I already told you I'm not familiar with ACF and never tried my self. So I can't get you the right solution but I can sure that you have to use update_post_meta() instead add_post_meta(). If you wanna use add_post_meta() then it will be repeated weirdly. and also try pass third parameter attachment ID. means -> update_post_meta($post_id, '_thumbnail_id', $attach_id); More importantly why don't you debug/var_dump() your return $value ? if you do then you will get some idea how to do this. – Mohammad Limon Sep 26 '16 at 22:57
  • Ok, what needs to happen is add another line in the function which sets $value to the medium size version of the image before the conditional if. – Joanna Mikalai Sep 27 '16 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.