Forked from mattradford/featured image from gallery.php
Created
August 1, 2024 23:43
-
-
Save dgoze/2b8700a4621502ba01012ad8b5edcc41 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.
This file contains hidden or 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
| function set_featured_image_from_gallery() { | |
| global $post; | |
| $post_id = $post->ID; | |
| $has_thumbnail = get_the_post_thumbnail($post_id); | |
| if ( !$has_thumbnail ) { | |
| $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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment