Skip to content

Instantly share code, notes, and snippets.

@kodie
Created December 5, 2018 20:24
Show Gist options
  • Save kodie/6b9948af2533e4f548f449f81bb5fdc2 to your computer and use it in GitHub Desktop.
Save kodie/6b9948af2533e4f548f449f81bb5fdc2 to your computer and use it in GitHub Desktop.
Migrate ACF field data to a taxonomy
<?php
$query = new WP_Query(array(
'post_type' => 'employee',
'posts_per_page' => -1
));
while($query->have_posts()) {
$query->the_post();
$taxonomy = 'department';
$term = get_field('department');
if (is_array($term)) $term = $term[0];
$term_info = term_exists($term, $taxonomy);
if (!$term_info) $term_info = wp_insert_term($term, $taxonomy);
if (!is_wp_error($term_info)) {
$term_id = $term_info['term_id'];
wp_set_post_terms(get_the_ID(), array($term_id), $taxonomy);
delete_field('department', get_the_ID());
} else {
var_dump($term_info);
}
}
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment