Last active
April 9, 2017 20:19
-
-
Save robwent/f3fdc580becdd3d56cc50f632e210f0c to your computer and use it in GitHub Desktop.
Sets an image associated with a post as featured if nothing is set
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
<?php | |
function auto_featured_image() { | |
global $post; | |
if (!has_post_thumbnail($post->ID)) { | |
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1&order=ASC&orderby=ID" ); | |
if ($attached_image) { | |
foreach ($attached_image as $attachment_id => $attachment) { | |
set_post_thumbnail($post->ID, $attachment_id); | |
} | |
} | |
} | |
} | |
// Use it temporary to generate all featured images | |
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