自作フォームからのカスタムフィールド(タクソノミー)への登録
-
自分で作成したフォームで入力・選択した値をカスタムフィールドに登録させようとしていますがフィールドタイプがタクソノミーでセレクトボックスの場合が登録出来ません。
カスタムフィールドを登録する際の関数として、「update_post_meta」と「update_field」を使用してみましたがどちらも駄目でした。
下記にコードを記載します。
[update_post_metaの場合]
———————————————————————————–add_action('template_redirect', 'insert_data'); function insert_data() { if(is_page('insert_page')) : $insert_post_data = $_POST // フォームで入力・選択した値を代入 $post_data['post_title'] = 'タイトル'; $post_data['post_status'] = 'publish'; $post_data['post_type'] = 'カスタム投稿タイプ'; $post_id = wp_insert_post($post_data); if(!is_wp_error($post_id)) : $acf_id = 'カスタムフィールドid'; $get_acf_fields = get_acf_all_fields_in_group($acf_id); // カスタムフィールドを全て取得 foreach($get_acf_fields as $key => $value) : $acf_name = $value['name']; // カスタムフィールド名を代入 if(isset($insert_post_data[$acf_name])) : // カスタムフィールドに入力する値があるか判定(セレクトボックスの場合、タームIDが入っている。) update_post_meta($post_id, $acf_name, $insert_post_data[$acf_name]); // カスタムフィールドを登録 endif; endforeach; endif;
———————————————————————————–
[update_fieldの場合]
———————————————————————————–add_action('template_redirect', 'insert_data'); function insert_data() { if(is_page('insert_page')) : $insert_post_data = $_POST // フォームで入力・選択した値を代入 $post_data['post_title'] = 'タイトル'; $post_data['post_status'] = 'publish'; $post_data['post_type'] = 'カスタム投稿タイプ'; $post_id = wp_insert_post($post_data); if(!is_wp_error($post_id)) : $acf_id = 'カスタムフィールドid'; $get_acf_fields = get_acf_all_fields_in_group($acf_id); // カスタムフィールドを全て取得 foreach($get_acf_fields as $key => $value) : $acf_name = $value['name']; // カスタムフィールド名を代入 if(isset($insert_post_data[$acf_name])) : // カスタムフィールドに入力する値があるか判定(セレクトボックスの場合、タームIDが入っている。) $field_key = $get_acf_all_fields[$acf_name]['key']; $value = array($acf_name => $insert_post_data[$acf_name]); update_field($field_key, $value, $post_id); // カスタムフィールドを登録 endif; endforeach; endif;
———————————————————————————–
- トピック「自作フォームからのカスタムフィールド(タクソノミー)への登録」には新たに返信することはできません。