0

I have over 1500 posts I imported from Tumblr. While the images were imported into the gallery, I need to make sure there's a featured image for each. How do I make the images (12 per post minus 30 that don't have images) a featured image automatically?

4

You can run a script that will programmatically set the featured image for each post. You can use the first attached image for this. To get the attached images run a query for the posts and loop through each one and use get_children() setting the post_parent to the current post id in your loop.

$posts = get_posts( array( 'posts_per_page' => -1 ));

foreach ( $posts as $post ) {

   $featured = wpse_get_attachments( $post->ID );
   foreach( $featured as $attachment ) {
     $img = set_post_thumbnail( $post->ID, $attachment['ID'] );
   }
   if ( $img )
       echo 'Featured image set for '. $post->ID; 
}
/**
 * Queries for attached images and returns first.
 *
 * @param int $post_id The post id to check if attachments exist
 * @return array|bool The 1st attached on success false if no attachments
 */
function wpse_get_attachments( $post_id ) {
    return get_children( array(
            'post_parent'    => $post_id,
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'posts_per_page' => (int)1
        ), ARRAY_A
    );
}

You would need to put this code somewhere it can get executed like a custom page template that you would load only one time to set the featured images.

You could also use a plugin I wrote that will do this via an admin tools page via ajax. http://wordpress.org/extend/plugins/media-tools/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.