Home › Forums › Backend Issues (wp-admin) › Set image as featured image
Okay, Now I have this and it still does not work, what’s going on?
//Auto assign Featured image from 'post_image'
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
update_post_meta($post_id, '_thumbnail_id', $value->ID);
}
return $value;
}
add_filter('acf/update_value/key=field_53b58efe0accf', 'acf_set_featured_image', 10, 3);
Strange right? I tested it with field output->ID and just using $value
that works great… but I need the object
It just hit me that the object output value might be handled by a function that kicks in on get_field etc. so the value saved are probably always just the ID.. So give it a try. Keep the object setting but use just $value.
haha true! that did the trick.
Stupid that I didn’t even try your code at first!
Thanks anyway! p.s. I would really appreciate if you give your experienced view on my question here
Thanks Jonathan!
Hi,
Thank you for the detailed feed. I have been struggling to get this working on my site for the past hour.
I am trying to set the first image in a repeater row as the featured image for each existing and new post.
The repeater is called sock_image and the image field is called sock_images. Any clue on what I am doing wrong? This is what I put in my functions file:
// Set the first sock image uploaded as the featured image
function acf_set_featured_image( $value, $post_id, $field ){
if($value != ''){
delete_post_thumbnail( $post_id);
//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=sock_images', 'acf_set_featured_image', 10, 3);
That’s because your $value is a repeater field rather than a single image field.
However I’m not sure wether $value will be the entire repeater or just the row count which is actually saved to the database for a repeaterfield.
Thank you for the quick response.
I am not that advanced when it comes to ACF (excuse the pun). Do you know how I could make the value the single image field?
Well if $value is the entire repeater you could do something like this:
// Set the first sock image uploaded as the featured image
function acf_set_featured_image( $value, $post_id, $field ){
if(isset($value) && is_array($value)){
delete_post_thumbnail( $post_id);
//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[0][sock_image]);
}
return $value;
}
// acf/update_value/name={$field_name} - filter for a specific field based on it's name
add_filter('acf/update_value/name=sock_images', 'acf_set_featured_image', 10, 3);
That is if you’ve set the return value of the image field to URL. Otherwise you’ll have to do a bit more with it..
But really you should activate wp_debug and do an error_log($value);
inside the function to find out what value is first.
Hi there
Interesting post, and nearly what I need, but I can’t understand some things.
when calling the function it passes the variables ’10” and ‘3’.
add_filter(‘acf/update_value/name=sock_images’, ‘acf_set_featured_image’, 10, 3);
being the post id and the post field.
So this would only set the one image from that one post to featured.
I have a custom field called ‘gallery images’ with just one image upload field.
As I add a new gallery image, each image has a new post id. meaning the code you used to set each image as featured would not work.
I am using a gallery plugin that requires all images in custom post types to be featured.
Is this possible at all?
I would like to use the ‘gallery’ field option, or a simple repeater field.
Any help on how to achieve this would be grand.
It would be a nice feature to add in the future, a simple check box when you insert an image field ‘set this image as featured’
Thanks in advance.
Awesome plugin by the way
I need to set each of the images being uploaded to featured, not just one.
Hi @charis
I’m not quite following you. You have a CPT in which you’ve created a single image field? And when saving that you want it to become the featured image?
If so, why not just use the featured image meta box provided by WordPress.
Hi there
Thanks for your swift reply.
One image field was just as an example. (I could use the original WP featured image box, but it is really ugly and would require me to make a new post for each image)
I actually need to use the repeater field, repeating an image field. And have each of the images be set to featured.
So say I have one CPT called ‘gallery’, with a ACF ‘gallery images’ (with a repeater field, with image, title, description)
I only want to add one gallery, one post, with as many images as I want inside that one post, and all set to featured image.
Hope this is a little more clear.
If possible I would like to use the gallery field to do the same thing as well.
The problem I see with the code you supplied earlier in this post was that it only uses a set post and field id. And that would constantly change with multiple images needing to be set to featured.
Any help would be great
Hi Jonathan,
We are going to use ACF to create a new featured image to replace the existing core featured image in posts. We have an existing multisite with Polylang. The code is working great on our development site. It replaces, updates, removes, etc etc .. works great.
But.. it doesn’t show the current selected core featured image when you edit the post the first time. When you click edit on a post it doesn’t show a featured image for the new ACF featured image even though the post has one attached to it.
How can we put the existing core image into the ACF image when you click edit the first time?
// REPLACE FEATURED IMAGE WITH ACF
function acf_set_featured_image( $value, $post_id, $field ){
update_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=pfimage’, ‘acf_set_featured_image’, 10, 3);
// hide existing featured image
add_action(‘admin_head’, ‘my_admincss’);
function my_admincss() {
echo ‘<style>
#postimagediv.postbox {
display: none !important;
}
</style>’;
}
// END REPLACE FEATURED IMAGE WITH ACF
Thanks,
David
The topic solution works wonderfully. However, I noticed that when looking in the media browser the images, although assigned as thumbnails, are not “attached” to any particular posts.
Is there a way the image can be “attached” to the post too?
I have a plugin that deletes all associated post content upon post deletion (all attached media), which is why I’d like to get this working.
Many thanks 🙂
Hello,
I was searching for how to add a feature image through ACF Form, but I ended up here, and this helped me to find the solution.
I hope this helps you.
add_action('acf/save_post', 'acf_set_featured_image');
function acf_set_featured_image($post_id){
$value = get_field('post_image', $post_id);
if($value != ''){
add_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
This action check if you updated the field “post_image” from a frontend form and update the feature image with the same image.
I know this is an old topic, but after doing this for some time I’ve just recently realized that using a filter isn’t needed. All you really need to do is create an image field with the field name of “_thumbnail_id”, and ACF will just update the featured image for you. There’s no error checking, but simply making your image field required deals what that problem.
Hi John, I just wanted to say thanks for posting this, there are threads all over the place about this topic and no documentation in the acf form pages. If Acf has a front end form and can load content and title into it, adding a featured image should come as standard. I wasted an entire day looking for a solution to this, your simple solution solved my issue straight away.
As I’ve probably posted in other topics where I’ve made similar comments, I also have done it with filters, front end and back. The simple solution hit me in the head like a sledge hammer when I finally saw it. Sometimes the easiest solutions are also the easiest to overlook.
still not working for me. i dont know why.
my custom field is name is ‘large-image’
and it links a jpg hosted somewhere else not on my site…
Looking to migrate 2k posts to a new theme :/
Hi @jonathan , I have read this topic and tried to adapt it to my needs but it is not working. Maybe you could help me our here. I have a CPT with some images. I would like to define of of the images as my featured image. The image is called “1_bild” and I have set it as an image-array at the ACF configuration. I have used the following code but it does not work for me:
//Auto assign Featured image from 'post_image'
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
update_post_meta($post_id, '_thumbnail_id', $value->ID);
}
return $value;
}
add_filter('acf/update_value/key=1_bild', 'acf_set_featured_image', 10, 3);
What am I doing wrong here?
Thanks a lot!
The value that is passed to your filter is the image ID and not an array or an object. ACF only uses the return value when using get_field() or one of the other functions for getting the value.
update_post_meta($post_id, '_thumbnail_id', $value);
Hi John, thanks a lot! I also tried this but no featured image is shown. I check the featured image with the following code:
<?php
// check if the post or page has a Featured Image assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
This is probably a stupid question. But I have defined $value in the functions.php with the following:
$value = get_field('1_bild', $post_id);
Is this correct? @John
I just noticed your filter
add_filter('acf/update_value/key=1_bild', 'acf_set_featured_image', 10, 3);
1_bild is not a valid field key, that is the field name, so your filter is probably never run. It should probably be
add_filter('acf/update_value/name=1_bild', 'acf_set_featured_image', 10, 3);
Also, not sure if this will effect anything or not but it’s generally not a good idea to use a field name that begins with a number. This all depends on what you plan to do with the name though so in most cases it won’t matter.
Hi John, thanks for the tipp! I tried to change from key to name but no difference. I also changed the name of the acf image to “bild” and adapted the code – but affects. I don’t understand why .. :/
//Auto assign Featured image from 'post_image'
function acf_set_featured_image( $value, $post_id, $field ){
$value = get_field('bild', $post_id);
if($value != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
update_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
add_filter('acf/update_value/key=bild', 'acf_set_featured_image', 10, 3);
Hi!
How to do the same for oembed (Youtube) field? Making Youtube thumbnail as the featured image on page save.
OMG @hube2 – brilliant.
Just using _thumbnail_id
for the field name is so easy and makes everything work perfectly. You should pin this at the top of the thread.
Using the filter method and a custom field name:
Just using _thumbnail_id
for the field name makes all of the above scenarios work perfectly.
This makes it real easy & nice to use 'hide_on_screen' => 'featured_image'
to hide the default entry meta box and then place the _thumbnail_id
field wherever it makes sense within the ACF form workflow.
The topic ‘Set image as featured image’ is closed to new replies.
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.