原先在 Post 上開了 post_must_read_department 欄位,想要轉移資料到新開的 read-department
欄位。
read-department
是 Taxonomy 分類法,所以我們會需要取得實際的分類法編號做更新
$posts = get_posts(['posts_per_page' => -1, 'fields' => 'ids']);
foreach ($posts as $post) {
$departments = get_field('post_must_read_department', $post);
$departments = array_map('strtolower', $departments);
$terms = [];
foreach ($departments as $department) {
$terms[] = get_term_by('name', $department, 'read-department');
}
$terms = array_map(function ($term) { return $term->term_id; }, $terms);
update_field('post_must_read_department_term', $terms, $post);
}