Need to take the first image out of posts and set it as the featured image

To get you started:

Breaking your goal into smaller tasks will help a lot in figuring out how to do this. You probably want to:

  1. Do a custom query that finds all published posts
  2. Use a foreach loop for your query results
  3. Within the loop, find the first image
  4. Set that first image as the featured image (save it as postmeta)
  5. Remove the image from that post’s content

You should be able to work out #1 and #2 on your own. For #3, here is code you would place within the foreach loop that will identify the first <img> tag – but you’ll want to make sure that you only have images from this site within your posts, because it will grab any <img> within the post, so if you have embedded images from other sites you’ll have to make the regex more complex to find the first image from your own domain. This code assumes you have saved your query results to a variable called $item:

// get the full content of the post
$content = $item->get_content();
// find the first <img> tag and save to variable $imageUrl
preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $imageUrl);
// [1] is just the image URL. [0] is most but not all of the <img> tag.
$imageUrl = $imageUrl[1];

Now that you have the URL of the image, you’ll need to reverse engineer the attachment ID so that you can then assign that attachment ID as the featured image for your post. Pippin has an excellent little function to do this part:

function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
        return $attachment[0]; 
}

Place this function above or below your loop, not inside of it, and then back within your loop right after you set $imageUrl:

$imageId = pippin_get_image_id($imageUrl);

Here’s an example of setting the featured image programmatically.

Hope this points you down the right path. Play around with it a bit and if you get stuck on a specific part you can come back and post a more specific question about that part.