PHP 将 WP 术语 child 术语导入自定义 post 类型
PHP Import WP Terms child term into custom post type
我创建了一个导入工具来从 json 文件导入数据,以在我的自定义 post 类型中创建 posts。一切正常,我可以导入 acf 字段以及分配的术语。我的问题是,如何导入分配给术语的 child 术语。
我现在有两个收集数据的变量。我希望有变量 eventCategoryChildName 将其 vaule 作为 child 术语分配给 eventCategoryID
$eventCategoryChildName = $ev['OfficialCity'];
$eventCategoryId = $ev['RegionID'];
以下是在没有 child 个术语的情况下术语的导入功能:
// 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 );
编辑:
所以我设法导入了关联到右侧 parents 的 child 但它们没有检查到 post
// 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 );
}
首先,您应该检查 wp_set_object_terms
的结果,看看是否有错误 returned:
$term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
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.
}
那么,您要添加的字词可能不存在。在这种情况下,wp_set_object_terms
将 return 一个 invalid_taxonomy
错误。因此,您可能希望将此类缺失的术语添加到您的分类法中:
wp_insert_term(
'Term Name', // Customize as you wish
'lan',
array(
'description' => 'Term desc', // Customize as you wish
'slug' => 'lan-slug1' // Customize as you wish
)
);
// For child term:
$parent_term = term_exists( 'parent_term_id', 'lan' );
$child_term_result = wp_insert_term(
'Child Term Name', // Customize as you wish
'lan',
array(
'parent' => $parent_term['term_id']
'description' => 'Term desc', // Customize as you wish
'slug' => 'lan-slug1' // Customize as you wish
)
);
// Add the child term to the post
if ( !is_wp_error( $child_term_result ) ) {
wp_set_object_terms( $post_id, $child_term_result['term_id'], 'lan', true );
}
最后,请确保您至少在 Wordpress 的 init
钩子之后执行此代码。例如:
function parse_my_json() {
// Your code here
}
add_action('init','parse_my_json');
有用的参考资料:
我创建了一个导入工具来从 json 文件导入数据,以在我的自定义 post 类型中创建 posts。一切正常,我可以导入 acf 字段以及分配的术语。我的问题是,如何导入分配给术语的 child 术语。
我现在有两个收集数据的变量。我希望有变量 eventCategoryChildName 将其 vaule 作为 child 术语分配给 eventCategoryID
$eventCategoryChildName = $ev['OfficialCity'];
$eventCategoryId = $ev['RegionID'];
以下是在没有 child 个术语的情况下术语的导入功能:
// 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 );
编辑:
所以我设法导入了关联到右侧 parents 的 child 但它们没有检查到 post
// 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 );
}
首先,您应该检查 wp_set_object_terms
的结果,看看是否有错误 returned:
$term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
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.
}
那么,您要添加的字词可能不存在。在这种情况下,wp_set_object_terms
将 return 一个 invalid_taxonomy
错误。因此,您可能希望将此类缺失的术语添加到您的分类法中:
wp_insert_term(
'Term Name', // Customize as you wish
'lan',
array(
'description' => 'Term desc', // Customize as you wish
'slug' => 'lan-slug1' // Customize as you wish
)
);
// For child term:
$parent_term = term_exists( 'parent_term_id', 'lan' );
$child_term_result = wp_insert_term(
'Child Term Name', // Customize as you wish
'lan',
array(
'parent' => $parent_term['term_id']
'description' => 'Term desc', // Customize as you wish
'slug' => 'lan-slug1' // Customize as you wish
)
);
// Add the child term to the post
if ( !is_wp_error( $child_term_result ) ) {
wp_set_object_terms( $post_id, $child_term_result['term_id'], 'lan', true );
}
最后,请确保您至少在 Wordpress 的 init
钩子之后执行此代码。例如:
function parse_my_json() {
// Your code here
}
add_action('init','parse_my_json');
有用的参考资料: