我已经处理了一个类似的需求,到目前为止,最好的方法是使用自定义字段来保存相关的术语id。
这意味着对于每个“实践领域”职位类型,将有一个“专业术语id”自定义字段,其中“专业”术语id为值。
这里是为每个帖子创建术语的操作挂钩
add_action( \'save_post\', \'update_related_term\');
function update_related_term($post_id) {
$post_type_as_taxonomy = array(\'practice-area\');
$post = get_post( $post_id );
if(in_array($post->post_type, $post_type_as_taxonomy) && $post->post_status==\'publish\'){
$term_args[\'name\'] = $post->post_title;
$term_args[\'slug\'] = $post->post_name.\'\';
$term_id = get_post_meta($post_id, $post->post_type.\'-term-id\', true);
if($term_id){
$term = wp_update_term( $term_id, $post->post_type.\'-term\', $term_args );
} else {
$term = wp_insert_term( $term_args[\'name\'], $post->post_type.\'-term\', $term_args );
$meta_status = add_post_meta($post_id, $post->post_type.\'-term-id\', $term[\'term_id\'], true);
}
}
}
以及删除每个帖子上的术语的操作删除
add_action(\'admin_init\', \'codex_init\');
function codex_init() {
if (current_user_can(\'delete_posts\')){
add_action(\'before_delete_post\', \'delete_related_term\', 10);
}
}
function delete_related_term($post_id) {
$post_type_as_taxonomy = array(\'practice-area\');
$post = get_post( $post_id );
if (in_array($post->post_type, $post_type_as_taxonomy)) {
$term = get_post_meta($post_id, $post->post_type.\'-term-id\', true);
wp_delete_term( $term, $post->post_type.\'-term\');
}
}
注意,我使用“实践领域”作为自定义帖子类型,“实践领域术语”作为相关分类法。
希望这有帮助