WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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/

share|improve this answer
Thank you very much. I will try both suggestions! – Cecily Jan 9 '13 at 3:24
Thank you your plugin worked beautifully! – Cecily Jan 10 '13 at 2:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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