Support

Account

Home Forums General Issues Set featured image of CPT from existing image

Solving

Set featured image of CPT from existing image

  • Hello,

    I have a CPT with a certain image field which I want to set as ‘featured image’ of the post. I am currently using a plugin that sort of does that (https://wordpress.org/support/plugin/default-thumbnail-plus) but it is dynamic, meaning that once I turn it off, there is no featured image set for those CPTs any more.

    Is there a way I could actually copy the content from image field “X” to the featured image field set by WP?

    edit1: my image is “image array”
    edit2: I just saw this thread: http://support.advancedcustomfields.com/forums/topic/featured-image-not-displaying/ but I still wasn’t able to make it work in my case.

    thanks a lot

  • Yes, there is. You create an acf/save_post action http://www.advancedcustomfields.com/resources/acfsave_post/

    In your function you get the value of the field which is stored as an ID. You can either do it with ACF using $image = get_field('image_field', $post_id, false); which will return the ID value or you can use $image = get_post_meta($post_id, '', true);

    then you do

    
    $image = intval($image); // make sure it's an int
    set_post_thumbnail($post_id, $image);
    

    But this won’t be retroactive, you’d need to update every post.

  • hi,
    thanks, correct me if I am wrong but this will work only as long as the function exists?

    is there any way which I could force all the existing entries on my db to be updated like this? In layman terms, copy whatever you see at field “the_logo” to the WP featured image field 😀

  • get_post_meta() and set_post_thumbnail() always exist. The problem you’d run into is if ACF is not installed and this would work only when you actually updated the post in the admin.

    Would it be possible to do this to every post? Yes, but it’s far more complicated and may not actually work. If there are many posts it could time out before it completes. The other question is “When do you run it?” What will trigger it to run. It’s not something you’d want to do every time a page is loaded or even every time the admin is loaded.

    To do it all at once you’d need to get all the posts, loop through them and get the custom field for each and then update the featured image.

    I don’t really see any easier way to do it. The only thought I have is to somehow hook into WP where it get’s the information for each post, before it actually gets the post and do it then… but that may actually cause an infinite loop.

    You might be able to use the ‘the_post’ hook to create an action that would run on each post when it’s loaded. Check it there and do the update then. But I’m not really sure what you’d need to do with this.

  • Hi,

    I am also interested in automatically generate thumbnail
    I put this piece of code in functions.php but it does not work when I record Article
    Maybe it’s because I’m in the admin?
    1 / How to build this miniature that is either front-end or back office?
    2 / Is optimizable by optimization plugin (weight / height of the image)?
    Or is it possible to provide an image for the thumbnail?
    Regards

    function my_acf_save_post( $post_id ) {
        
        // get new value
     //   $value = get_field('my_field');
        
      $image = get_field('image', $post_id, false);  //image est ACF field
      $image = intval($image); // make sure it's an int
      set_post_thumbnail($post_id, $image);
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • @hdcms, The featured image in WP only takes an attachment ID, which point to the image object and not to a specific image size. If you want to show different size images on the front then then you’ll need to create the code that does it.

    Look at https://codex.wordpress.org/Function_Reference/the_post_thumbnail

  • i take it this is a one off, meaning that you want to rip through the whole list of posts and assign the field X image to the featured image.

    Why dont you write a script (one run) that will load in all posts for assigning retroactively, check for the custom image, if it exists, set it as the thumbnail of the post.

    NOTE* whenever writing custom scripts like this, backup your database and when the script has run successfully, delete the file from your server.

    something like this, sitting in the root would probably work, this was written of the top of my head so check the code.

    <?php
    require('wp-load.php'); // get the wp api
    
    $args = array(
    'post_type' => 'your_post_type',
    'posts_per_page' => -1
    );
    
    $queryWP = new WP_Query($args);
    while ($queryWP->have_posts()) : $queryWP->the_post();
    
    // get the post id
    $postID = get_the_ID();
    // get the custom field image as an object
    $field_image = get_field("your_field_key");
    // get the image id
    $imageID = $field_image['ID'];
    // check if the post does not have a featured image and that the image id is not empty
    if(!has_post_thumbnail($postID) && $imageID!=''):
    // set the post featured image
    set_post_thumbnail( $postID , $imageID );
    // output confirmation
    echo "post set - ".$postID."<br>"
    echo "thumbnail id as featured image - ".$iamgeID."<br>";
    endif;
    
    endwhile;
    ?>
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Set featured image of CPT from existing image’ is closed to new replies.