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 been working with featured images more lately and I been adding them to my posts as I go. However I have a bunch of older posts that do not have the featured image set and I want to use it for such things as wptouch etc.

So I have like 300 posts and to be honest I dont want to go into 300 posts and set the images manually.

IS there anyway either by plugin or code that you can code it where the featured images area will grab the first image in each post?

share|improve this question

if the image is inserted (attached) into the post and not a external link:

function get_my_thumbnail($post_id, $size, $attr = ''){

  // check of featured image first
  $t = get_post_thumbnail_id($post_id);

  // no featured image set, check post attachments
    $attachments = get_children(array(
      'post_parent'    => $post_id,
      'post_status'    => 'inherit',
      'post_type'      => 'attachment',
      'post_mime_type' => 'image',
      'order'          => 'ASC',
      'orderby'        => 'menu_order ID',
    $attachment = array_shift($attachments); // we only need one
    $t = $attachment ? $attachment->ID : false;

  // no attachments either...
  if(empty($t)) return 'no image...';

  // we have either attachment / featured image, so output the post thumbnail
  do_action('begin_fetch_post_thumbnail_html', $post_id, $t, $size); // compat
  $html = wp_get_attachment_image($t, $size, false, $attr);
  do_action('end_fetch_post_thumbnail_html', $post_id, $t, $size);

  return $html;

You may want to also regenerate all attachment sizes (auto-create $size-d images from your older ones), so you don't get browser-resized huge images...

share|improve this answer
and on another note how do i give rep out for people helpng me out? – dclc Aug 26 '11 at 15:23
how does it work with external images? i.e. not external, but residing on the server – Toskan Feb 25 '14 at 13:13
Hi @onetrickpony. When I add this code to my theme's functions.php I receive the error PHP Parse error: syntax error, unexpected ')', expecting variable (T_VARIABLE) or '$' in /var/www/vhosts/whitewreath.com/httpdocs/wp-content/themes/stealth-child/functio‌​‌​ns.php on line 34 – Steve Mar 22 at 5:21
try now, there were some typos in the code – onetrickpony Mar 22 at 7:42
Great, thanks @onetrickpony. I've added it to my theme's functions.php. I've also added it to my child theme's archive.php. However, no thumbnails are showing on my post archive page. Please forgive my ignorance, I'm not a developer. – Steve Mar 25 at 13:49

Here is the function:

function catch_that_image() {
global $post, $posts;
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

// no image found display default image instead
    $first_img = get_bloginfo('template_url')."/images/no_image.gif";
return $first_img;

and Here is how to use it:

$imgURL = catch_that_image(); 
share|improve this answer
yeah my images are not attachments but url links but on the same domain... will the abov e work and exactualy how do i run that code? for the programming unsavy? – dclc Aug 26 '11 at 15:22
this lets you extract the img src path. This does not solve OP question – Toskan Feb 25 '14 at 13:12
Please attribute the source of this function. – Hendrik Luehrsen Mar 22 at 8:45

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.