LoginSignup
0
0

More than 3 years have passed since last update.

Shellscriptを使ってページなどを大量に作成するときcsvから読み込む

Last updated at Posted at 2020-10-20

csvファイルの用意

エクセルとかGoogleスプレッドシートとかでクライアントから提出してもらったものや自分でまとめたものをcsvで書き出す。

会社概要,company
経営理念,philosophy
沿革,history
そのた,etc
.
.
.

shellscriptの作成

自分のローカル開発環境下ではvccw(古いのですが・・)のwordpressのコアファイル直下にtmpなどのディレクトリを作成しそこへshellscriptを配置。
post typeを変更する場合は"wp post create" のオプションなどを調べると色々指定できるはずです。
https://developer.wordpress.org/cli/commands/post/create/

複数指定の場合

PageSlug=`echo ${line} | cut -d ',' -f 2`

の箇所を追加して読み込む列に[+1]する。



#!/bin/bash
# set -ex
CSV_FILE=title_slug.csv
for line in `cat ${CSV_FILE}`
 do
  PageName=`echo ${line} | cut -d ',' -f 1`
  PageSlug=`echo ${line} | cut -d ',' -f 2`
  PostType=("page") # このpage部分をpostやカスタム投稿に指定
  # echo "${PageName}  ${PageSlug} ${PostType}" # テスト
  vagrant ssh -c "wp post create --post_type=${PostType} --post_author=admin --post_status=publish --post_title=${PageName} --post_name=${PageSlug}"
done
vagrant ssh -c "wp post list --post_type=${PostType}"

shellscriptの実行

csvも同じディレクトリへ配置し実行。

cd wordpress/tmp #配置したディレクトリへ移動
sh shellscript_filename.sh
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0