php 将ACF字段数据迁移到分类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将ACF字段数据迁移到分类相关的知识,希望对你有一定的参考价值。

<?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();
?>

以上是关于php 将ACF字段数据迁移到分类的主要内容,如果未能解决你的问题,请参考以下文章