function auto_post_thumbnail_image()
{
    global $wpdb;
    global $post;
    $post_id = $post->ID;
    if (get_post_meta($post_id, '_thumbnail_id', true) || get_post_meta($post_id, 'skip_post_thumb', true)) {
        return;
    }
    $post = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE id = {$post_id}");
    $matches = array();
    preg_match_all('/<\\s*img [^\\>]*src\\s*=\\s*[\\""\']?([^\\""\'>]*)/i', $post[0]->post_content, $matches);
    if (empty($matches[0])) {
        preg_match('%(?:youtube\\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\\.be/)([^"&?/ ]{11})%i', $post[0]->post_content, $match);
        if (!empty($match[1])) {
            $matches = array();
            $matches[0] = $matches[1] = array('http://img.youtube.com/vi/' . $match[1] . '/mqdefault.jpg');
        }
    }
    if (count($matches)) {
        foreach ($matches[0] as $key => $image) {
            preg_match('/wp-image-([\\d]*)/i', $image, $thumb_id);
            $thumb_id = $thumb_id[1];
            if (!$thumb_id) {
                $image = substr($image, strpos($image, '"') + 1);
                $result = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE guid = '" . $image . "'");
                $thumb_id = $result[0]->ID;
            }
            if (!$thumb_id) {
                $thumb_id = fetch_thumbnail_image($matches, $key, $post[0]->post_content, $post_id);
            }
            if ($thumb_id) {
                update_post_meta($post_id, '_thumbnail_id', $thumb_id);
                break;
            }
        }
    }
}
function auto_post_thumbnail_image()
{
    global $wpdb;
    global $post;
    //$postが空の場合は終了
    if (isset($post) && isset($post->ID)) {
        $post_id = $post->ID;
        //アイキャッチが既に設定されているかチェック
        if (get_post_meta($post_id, '_thumbnail_id', true) || get_post_meta($post_id, 'skip_post_thumb', true)) {
            return;
        }
        $post = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE id = {$post_id}");
        //正規表現にマッチしたイメージのリストを格納する変数の初期化
        $matches = array();
        //投稿本文からすべての画像を取得
        preg_match_all('/<\\s*img [^\\>]*src\\s*=\\s*[\\""\']?([^\\""\'>]*)/i', $post[0]->post_content, $matches);
        //YouTubeのサムネイルを取得(画像がなかった場合)
        if (empty($matches[0])) {
            preg_match('%(?:youtube\\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\\.be/)([^"&?/ ]{11})%i', $post[0]->post_content, $match);
            if (!empty($match[1])) {
                $matches = array();
                $matches[0] = $matches[1] = array('http://img.youtube.com/vi/' . $match[1] . '/mqdefault.jpg');
            }
        }
        if (count($matches)) {
            foreach ($matches[0] as $key => $image) {
                //画像がイメージギャラリーにあったなら、サムネイルIDをCSSクラスに追加(イメージタグからIDを探す)
                preg_match('/wp-image-([\\d]*)/i', $image, $thumb_id);
                if (isset($thumb_id[1])) {
                    $thumb_id = $thumb_id[1];
                }
                //サムネイルが見つからなかったら、データベースから探す
                if (!$thumb_id) {
                    $image = substr($image, strpos($image, '"') + 1);
                    $result = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE guid = '" . $image . "'");
                    if (isset($result[0])) {
                        $thumb_id = $result[0]->ID;
                    }
                }
                //それでもサムネイルIDが見つからなかったら、画像をURLから取得する
                if (!$thumb_id) {
                    $thumb_id = fetch_thumbnail_image($matches, $key, $post[0]->post_content, $post_id);
                }
                //サムネイルの取得に成功したらPost Metaをアップデート
                if ($thumb_id) {
                    update_post_meta($post_id, '_thumbnail_id', $thumb_id);
                    break;
                }
            }
        }
    }
}