【发布时间】:2018-03-29 07:13:42
【问题描述】:
我创建了一个导入工具,用于从 json 文件导入数据,以创建自定义帖子类型的帖子。这一切都很好,我可以导入 acf 字段以及指定的术语。我的问题是,如何导入分配给该术语的子术语。
我现在有两个变量来收集数据。我希望有变量 eventCategoryChildName 将其值作为子项分配给 eventCategoryID
$eventCategoryChildName = $ev['OfficialCity'];
$eventCategoryId = $ev['RegionID'];
以下是目前在没有子术语的情况下导入术语的功能:
// set category terms
$cat_ids = $eventCategoryName;
// Add these categories, note the last argument is true.
wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
编辑:
所以我设法将孩子与正确的父母相关联,但他们没有被检查到帖子
// set category terms
$cat_ids = $eventCategoryName;
// Import category name
$term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
$parent_term = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
// Check terms import for errors
if ( is_wp_error( $term_taxonomy_ids ) ) {
// There was an error somewhere and the terms couldn't be set.
echo $return->get_error_message();
} else {
// Success! These categories were added to the post.
}
$parent_term = term_exists( $eventCategoryName, 'lan' );
wp_insert_term(
$eventCategoryChildName, // Customize as you wish
'lan',
array(
'parent' => $parent_term['term_id'],
'slug' => $eventCategoryChildName // Customize as you wish
)
);
if ( !is_wp_error( $child_term_result ) ) {
wp_set_object_terms( $post_id, $child_term_result['term_id'], 'lan', true );
}
【问题讨论】:
标签: php arrays wordpress import categories