Support

Account

Home Forums Backend Issues (wp-admin) Set image as featured image

Solved

Set image as featured image

  • Is it possible to set an image (uploaded with ACF) automatically as the featured image of a post? It would really help me big time!

    So far I haven’t been able to find a solution for this yet..

    Thanks,

    Wilco

  • Hi!

    I think you could do something like this:

    
    add_post_meta($post_id, '_thumbnail_id', $attachment_id);
    

    since the posts featured image is really just a meta value (of course the actual image has to be uploaded and have its own “post” in wp_posts)

  • Hi @wdekreij

    Yes, you could use the above code in the acf/update_value action (read docs for info) for the image field

  • I just went through http://www.advancedcustomfields.com/resources/filters/acfupdate_value/ and tested various ways of coding, but I don’t think I totally understand..

    Where would I need to put the add_post_meta-code you posted in relation to the example code at http://www.advancedcustomfields.com/resources/filters/acfupdate_value/?

  • So far I just can’t get it done.. I’m trying to get the field “cursusfoto” (see http://d.pr/i/ibk3) set as the featured image. I already set the return value at the ID (I assume that’s right), see http://d.pr/i/q6nJ.

    However, I simply have no clue how to manage this code-wise.. (I’m not a developer).

  • Hi @wdekreij

    This should about do it.. Place this in your functions.php

    
    <?php
     
    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=cursusfoto', 'acf_set_featured_image', 10, 3);
     
    ?>
    
    
  • That works – thanks!!

  • Would this also be possible to set different WordPress configs?

    More specifically, I’d like to set the title of a page with a custom form (see http://d.pr/i/VemJ), as well as setting the post_status (e.g. published/draft etc) based on a checkbox.

    Do you think this would be possible?

  • Hi @wdekreij

    Of course. Pretty much anything is possible with PHP and WordPress

  • Trying to make this work with repeater fields. I’ve tried it using the repeater field name, and also the subfield image field name. Neither work.

    Any ideas?

  • You should probably use the repeater field name.. however the code above will not magically work just setting that.

    First you need to check what you’ve got in the $value variable.. I’m not sure this works but try doing a print_r($value); to see the values being saved. In the working example above the $value is just a post attachment ID. In your case with a repeater field I imagine it’s something more in line of an array or object which you’ll need to fetch the right value from.

  • Hi,

    Is there someone who can give an example of what the $value should be because I can not get it working.

    Thanks
    //Asger

  • Hi Asger,

    What are you trying to achieve? To set the featured image from within a repeaterfield?

  • Hi Jonathan,

    I have a frontend form were users can submit posts, and then I want them to upload a featured image through the image upload (frontend), but it did not work.

    I saw the code I could add to the functions.php but could not get it working, so I ask for an eksempel of what the if($value != ”){ should be because I think that was the problem.

    But basic i want users to add a featured image (frontend).

    //Asger

  • Hi Asger22,
    On the frontend form show the image field you already created in a field group.
    In the example code above given by Jonathan change the ‘cursusfoto’ substring to the fieldname of your image field.
    That should do it.

  • I had some issues with the code from Jonathan in the case a post already has a thumbnail. (E.g: after modifying the contents of the custom image field)

    The solution that works for me is to put the line:
    delete_post_thumbnail( $post_id);
    before the line
    add_post_meta($post_id, '_thumbnail_id', $value);

  • Or you can just switch the add_post_meta to a update_post_meta and you’ll be fine regardless 🙂

  • @Jonathan
    Yes, that works fine and is a better approach I think. I’m rather new to wp-development: didn’t even know that function is there 🙂

  • Live and learn 😉 It’s definitely a better approach so it’s also my bad for not putting that in the original code solution

  • @Jonathan, Today I started learning about hooks because I did not understand your given code earlier today. Now I understand everything except the add_post_meta($post_id, '_thumbnail_id', $value); where does '_thumbnail_id' come from?

    Im trying to get the same thing to work, but I have an image field thats outputs as an object for other reasons. How can I make this code work with an image object field?

    Thanks!

  • @boriskamp1991.
    1. _thumbnail_id is the ID WordPress uses for the attached image term.
    2. If you are seeing an object then you asked for an object. See the info on get_fields() vs the_field() on http://www.advancedcustomfields.com/resources/

  • To elaborate on Violacase answer.

    The _thumbnail_id is the meta_key which the thumbnail ID is being saved with in the wp_postmeta table in your database. It’s being used by default by WordPress.

    And if you have the image output set to object you can just use $value->ID and you’re good to go!

  • Well, Jonathan… I’m impressed with your to-the-point ‘elaboration’
    I’ll wait for you to comment first before EVER reacting again in this thread :’)

  • Thanks both of you for your replies! it’s stange to me, I did a search for _thumbnail_id but did not find any documentation on it by WordPress so I thought it was something you created!

    Thanks for the heads-up on the $value->ID @Jonathan!
    I want my post thumbnail to update on existing posts as well as to add on new posts so I went to update_post_meta and removed the if statement, now I have this:

    
    //Auto assign Featured image from 'post_image'
    function acf_set_featured_image( $value, $post_id, $field  ){
      update_post_meta($post_id, '_thumbnail_id', $value->ID);
      return $value;
    }
    
    add_filter('acf/update_value/name=post_image', 'acf_set_featured_image', 10, 3);
    

    which is not working…. What am I missing? thanks again!

  • You can still keep the If statement since it’ll just make sure to only update the value if there is something to update with. Otherwise you’ll end up with no image if you do an update and haven’t set a new image in the form.

    Also make sure your field is truly named “post_image”. Or change it to go by the field key which is actually prefered since it’ll retain the functionality even if you change the field name later on.
    acf/update_value/key={$field_key}

    You can find the field key by heading to screen options (top right) in acf admin and checking show field keys.

Viewing 25 posts - 1 through 25 (of 55 total)

The topic ‘Set image as featured image’ is closed to new replies.