以下のコードをfunctions.phpに記入します。
require('wp-blog-header.php');
$fp = fopen('shisetsu_kankou.csv', 'r'); //csvファイルを開く
//以下自動投稿。contentの本文はCustom field templateのフォーマットを示す。
$post =array(
'post_author'=>1,
'post_content' => '[cft format=1]',
'post_status' => 'publish',
'post_title' => '金沢の観光名所',
);
//以下カスタムフィールドにcsvファイルの内容を代入する。ファイルが終了するまで繰り返される。
while( !feof($fp) ){
// $genre = post_custom('genre');
// $name = post_custom('name');
// $detail = post_custom('detail');
$csv = fgetcsv($fp);
$kankou = array(
"genre" => $csv[9],
"name" => $csv[13],
"detail" => $csv[10]
);
$postid =wp_insert_post($post);
if($postid != 0){
update_post_meta($postid,"genre",$kankou['genre']);
update_post_meta($postid,"name",$kankou['name']);
update_post_meta($postid,"detail",$kankou['detail']);
};
}
;
fclose($fp); // csvファイルを閉じる。
以下カスタムフィールドです。途中で書く中身を変更したため日本語と英語が対応していませんが、上記のプログラムには対応しています。
[genre] label = 施設名 type = textfield size = 20 class = number required [name] label = 電話番号 type = textfield size =20 class = required [detail] label = 概要 type = textarea rows=4 cols=40 tinyMCE = true htmlEditor = true class = required


