LoginSignup
1
2

More than 1 year has passed since last update.

Advanced Custom Fieldsのグループでupdate_fieldしたい

Posted at

はじめに

WordPressのAdvanced Custom Fieldsに、グループというフィールドも作成できて、こちらを使うと管理画面が見やすくなるので、気に入ってました。

そしてwp_insert_postを使って、投稿を作成し、その投稿に対してカスタムフィールドを設定するときは、ACFのupdate_fieldという関数を使えばできるのですが、
...はて、グループでやるときはどうしたらいいんだ?とつまずいたので、メモしておきます。

参考サイト

ACF公式

解決に至ったサイト
↑このサイトの「The documentation for this seems to be incomplete. By trail and error I used the following solution:」このコメントの書いてあるところです。

とりあえず、文章で書く

1)管理画面からグループのフィールドキーを確認する
2)グループの内容は名前をキーにした連想配列にする
3)update_fieldする

画面つきで説明

1)ACF管理画面にて、グループのフィールドキーを表示できるようにする
スクリーンショット_2022-07-04_13_16_07.png

2)グループの親のフィールドキーを確認する
スクリーンショット_2022-07-04_13_23_41.png

グループの子どもはこんな感じ
スクリーンショット_2022-07-04_13_23_21.png

サンプルコード

<?php
//保存したいデータ
$presentData = [
  'present_company' => $_POST['present_company'],
  'present_address' => $_POST['present_address'],
  'present_tel' => $_POST['present_tel'],
  'delivery_date' => $_POST['delivery_date'],
];

update_field( 'field_グループの親のやつ', '保存するグループの配列', '投稿ID' );

おしまい。

1
2
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
1
2