/ Published in: PHP
If a post is published without setting a featured image and there is an image attachment, this will select an image from the media library and set it as the featured image.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function set_featured_image($post_ID){ if(!has_post_thumbnail($post_ID)){ 'post_parent' => $post_ID, 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_mime_type' => 'image', ); if($image = get_children($args)) { add_post_meta($post_ID, '_thumbnail_id', $image->ID); } } } add_action("publish_post", "set_featured_image");