Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * put the code in functions.php
- * visit any admin page in wordpress
- * delete the code
- */
- function get_first_image($ID) {
- $args = array(
- 'numberposts' => 1,
- 'post_mime_type' => 'image',
- 'post_parent' => $ID,
- 'post_status' => null,
- 'post_type' => 'attachment'
- );
- $attachments = get_children( $args );
- if ($attachments) {
- foreach($attachments as $att) return $att->ID;
- }
- }
- function make_featured_images() {
- // CHANGE THE POST TYPE AS NEEDED
- $posts = get_posts( array('numberposts' => -1, 'post_type' => 'post') );
- foreach ( $posts as $post ) {
- $featured = get_the_post_thumbnail( $post->ID, 'thumbnail', null );
- // WILL NOT RUN IF POST ALREADY HAS A FEATURED IMAGE
- if ( !$featured ) {
- $feat = get_first_image($post->ID);
- if($feat) set_post_thumbnail( $post->ID, $feat );
- }
- }
- }
- add_action('init', 'make_featured_images');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement