WordPress Snippet + ACF
Forked from mattradford/featured image from gallery.php
Last active
January 2, 2020 12:54
-
-
Save ControlledChaos/e3dff42a041545e6702d16d8e4016bbf to your computer and use it in GitHub Desktop.
Set the featured image from the first image in an ACF gallery. Saves only for a specified post type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ccd_featured_image_from_gallery() { | |
global $post; | |
$post_id = $post->ID; | |
$has_thumbnail = get_the_post_thumbnail( $post_id ); | |
if ( ! $has_thumbnail ) { | |
$images = get_field( 'my_image_gallery', $post_id, false ); | |
$image_id = $images[0]; | |
if ( $image_id ) { | |
set_post_thumbnail( $post_id, $image_id ); | |
} | |
} | |
} | |
add_action( 'save_post_{$post-type}', 'ccd_featured_image_from_gallery' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment