function set_first_image_as_featured() { if (has_post_thumbnail()) { return; } $content = apply_filters('the_content', get_post_field('post_content', get_the_ID())); preg_match_all('//i', $content, $matches); if (!empty($matches[1][0])) { set_post_thumbnail(get_the_ID(), $matches[1][0]); } } add_action('the_post', 'set_first_image_as_featured');