wordpressのデータベースにphpから直接記事を格納する
ググってもあまり情報がなかったので記録として
記事はすべてwp_postsに格納されている。フィールド全てに値を入れる必要はなく以下ぐらいで格納しておけば良い
$stmt = $dbh->prepare("insert into wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_name, post_modified, post_modified_gmt) value (:post_author,:post_date,:post_date_gmt,:post_content,:post_titl e,:post_name,:post_modified,:post_modified_gmt)"); $stmt->bindValue(':post_author',$post_author,PDO::PARAM_INT); $stmt->bindParam(':post_date',$post_date,PDO::PARAM_STR); $stmt->bindParam(':post_date_gmt',$post_date_gmt,PDO::PARAM_STR); $stmt->bindParam(':post_content',$post_content,PDO::PARAM_STR); $stmt->bindParam(':post_title',$post_title,PDO::PARAM_STR); $stmt->bindParam(':post_name',$post_name,PDO::PARAM_STR); $stmt->bindParam(':post_modified',$post_modified,PDO::PARAM_STR); $stmt->bindParam(':post_modified_gmt',$post_modified_gmt,PDO::PARAM_STR); $stmt->execute();
パーマリンクをいじる場合guidフィールドにも値を入れる
カテゴリをつける場合、あらかじめ管理画面からカテゴリを作り、wp_term_taxonomyでカテゴリidを調べ、wp_term_relationshipsにwp_postsのidとカテゴリidを入れ、wp_term_taxonomyの記事数を修正するという流れ
//カテゴリ追加 $stmt = $dbh->prepare("insert into wp_term_relationships (object_id,term_taxonomy_id,term_order) values (:object_id,:term_taxonomy_id,:term_order)"); $stmt->bindValue(':object_id',$id,PDO::PARAM_INT);//wp_postsのid $stmt->bindValue(':term_taxonomy_id',category_id,PDO::PARAM_INT); //カテゴリid $stmt->bindValue(':term_order',0,PDO::PARAM_INT); $stmt->execute(); //taxonomy更新 $stmt = $dbh -> prepare("update wp_term_taxonomy set count =:count where term_taxonomy_id = 1");//カテゴリid $stmt->bindValue(':count',$count,PDO::PARAM_INT);//記事格納後の記事数 $stmt->execute();
各フィールドの意味とかは以下のリンク参考に