Skip to content

Instantly share code, notes, and snippets.

@mattradford
Last active April 5, 2019 06:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattradford/b4db20572c11780d3138 to your computer and use it in GitHub Desktop.
Save mattradford/b4db20572c11780d3138 to your computer and use it in GitHub Desktop.
Set the featured image from the first entry in an ACF gallery field, saving only for a specified post type.
@lestzx
Copy link

lestzx commented May 10, 2018

Is it possible to add a code to update a thumbnail after changing order in the "gallery" field?

@thedavedavies
Copy link

@lestzx - You can just remove if(!$has_thumbnail ) ie:

function set_featured_image_from_gallery() {

    global $post;
    $post_id = $post->ID;

    $has_thumbnail = get_the_post_thumbnail($post_id);

    $images = get_field('image_gallery', $post_id, false);
    $image_id = $images[0];

    if ( $image_id ) {
      set_post_thumbnail( $post_id, $image_id );
    }
}
add_action( 'save_post_property', 'set_featured_image_from_gallery' );

@hotelpress
Copy link

Would this work with Custom Post Types?

I have one called 'properties' - Do I need to change anything ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment