Skip to content

Instantly share code, notes, and snippets.

@caionorder
Created January 3, 2020 19:17
Show Gist options
  • Save caionorder/9d532be34532a8df1fcb43559c2ae1cc to your computer and use it in GitHub Desktop.
Save caionorder/9d532be34532a8df1fcb43559c2ae1cc to your computer and use it in GitHub Desktop.
<?php
function auto_featured_image() {
global $post;
if (!has_post_thumbnail($post->ID)) {
$attached_image = get_children( "post_parent=$post->ID&amp;post_type=attachment&amp;post_mime_type=image&amp;numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
add_action('the_post', 'auto_featured_image');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment