LoginSignup
6
5

More than 3 years have passed since last update.

Really Simple CSV Importer使用時にAdvanced Custom Fields の値とラベルをきっちり登録する。そして改行も反映する。

Posted at

困ってること

Advanced Custom Fieldsでカスタムフィールドに値とラベルを設定してある場合(チェックボックスやセレクトボックス、ラジオボタンなど)、Really Simple CSV Importerでcsvインポートする時は、カスタムフィールドの値だけを指定します。
しかしそれだとDBにはラベルが反映されていないため、表示した時に値からラベルを引っ張ってこれません。
管理画面から一記事ずつ更新をかければラベルも登録されますが、大量の記事がある場合面倒です。

またテキストエリアの場合、改行を「自動で<br>に変換」にしていても、csv登録した時点では改行が反映されていません。
これも管理画面から一記事ずつ更新をかければ反映されますがやはり面倒です。

やりたいこと

csvインポート時に値と一緒にラベルも反映し、改行も反映したい。

前提

  • Advanced Custom Fieldsでカスタムフィールド を登録している。以下ACFで略称。
  • csvインポートはReally Simple CSV Importerを使用。
  • Really Simple CSV Importerにはデータベースにインポートした後に投稿データを更新するアクションフック(really_simple_csv_importer_post_saved())が用意されているのでそれを利用。
  • チェックボックス の場合は、複数選択時の処理が入っていることが前提。
  • カスタムフィールド の更新はACFの独自関数update_field()を使用。

ラジオボタンの場合

フィールド名
animals

選択肢
cat : 猫
dog : 犬
bird : 鳥

カスタムフィールド の更新にupdate_field()を使用している点に注意。
Wordpressで用意されているupdate_post_meta()はACFのカスタムフィールド には反映されない。

function.php
/* ==========================================================================
csvインポート時の処理
========================================================================== */
function really_simple_csv_importer_all_update($post) {
  // 記事ID取得
  $_postID = $post->ID;

  $_animals = get_post_meta($_postID, 'animals');
  update_field( 'animals', $_animals, $_postID );

}
add_filter( 'really_simple_csv_importer_post_saved','really_simple_csv_importer_all_update', 5, 3 );

セレクトボックス(単一選択) の場合

フィールド名
pref

選択肢
hokkaido : 北海道
aomori : 青森県
iwate : 岩手県

ほとんどラジオボタンと同じですが、get_post_metaの第三引数を指定しています。ACFではセレクトボックスが配列で渡ってくるのか、ここをtrueにしないとうまく反映されないので注意。

function.php
/* ==========================================================================
csvインポート時の処理
========================================================================== */
function really_simple_csv_importer_all_update($post) {
  // 記事ID取得
  $_postID = $post->ID;

  $_pref = get_post_meta($_postID, 'pref',true);
  update_field( 'pref', $_pref, $_postID );

}
add_filter( 'really_simple_csv_importer_post_saved','really_simple_csv_importer_all_update', 5, 3 );

チェックボックスの場合

フィールド名
foods

選択肢
food_001 :寿司
food_002 : 天ぷら
food_003 : カレー

チェックボックス の場合は、複数選択時の処理が入っていることが前提。
以前記事にまとめたので参考に設定する。ACFでも同じ処理でOK。
Really Simple CSV Importerを使ってSmart Custom Fieldsのチェックボックスをインポートする方法

ここもほとんどラジオボタンと同じですが、get_post_metaの第三引数を指定しています。

function.php
/* ==========================================================================
csvインポート時の処理
========================================================================== */
function really_simple_csv_importer_all_update($post) {
  // 記事ID取得
  $_postID = $post->ID;

  $_foods = get_post_meta($_postID, 'foods',true);
  update_field( 'foods', $_foods, $_postID );

}
add_filter( 'really_simple_csv_importer_post_saved','really_simple_csv_importer_all_update', 5, 3 );

テキストエリアの場合

フィールド名
comment

改行
自動的に<br>に変換

ここもほとんどラジオボタンと同じですが、get_post_metaの第三引数を指定しています。

function.php
/* ==========================================================================
csvインポート時の処理
========================================================================== */
function really_simple_csv_importer_all_update($post) {
  // 記事ID取得
  $_postID = $post->ID;

  $_comment = get_post_meta($_postID, 'comment',true);
  update_field( 'comment', $_comment, $_postID );

}
add_filter( 'really_simple_csv_importer_post_saved','really_simple_csv_importer_all_update', 5, 3 );

まとめると

function.php
/* ==========================================================================
csvインポート時の処理
========================================================================== */
function really_simple_csv_importer_all_update($post) {
  // 記事ID取得
  $_postID = $post->ID;

  //ラジオボタンの処理
  $_animals = get_post_meta($_postID, 'animals');
  update_field( 'animals', $_animals, $_postID );

  //セレクトボックスの処理
  $_pref = get_post_meta($_postID, 'pref',true);
  update_field( 'pref', $_pref, $_postID );

  //チェックボックスの処理
  $_foods = get_post_meta($_postID, 'foods',true);
  update_field( 'foods', $_foods, $_postID );

  //テキストエリアの処理
  $_comment = get_post_meta($_postID, 'comment',true);
  update_field( 'comment', $_comment, $_postID );

}
add_filter( 'really_simple_csv_importer_post_saved','really_simple_csv_importer_all_update', 5, 3 );

サーバーの負荷に注意

記事がインポートされるたびに一記事ずつアクションフックが起動するので、一気に大量の記事をインポートする際は要注意。
csvを分割するなどして適宜対応してください。

6
5
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
6
5