我有一个功能,每当我在自定义帖子类型“美术家”中添加新帖子时,它都会在自定义分类法“美术家”中添加一个同名的新术语。
function add_artist_category_automatically($post_ID) {
global $wpdb;
if(!has_term(\'\',\'byartist\',$post_ID)){
$cat = get_the_title($post_ID);
wp_set_object_terms($post_ID, $cat, \'byartist\');
wp_set_object_terms($post_ID, NULL, \'byartist\' );
}
}
add_action(\'publish_artists\', \'add_artist_category_automatically\');
现在,我想要一个函数,每当我从“艺术家”自定义帖子中丢弃帖子时,它都可以删除与自动添加函数创建的相同slug的术语。我还没有做到这一点:
function delete_artist_term_automatically($post_ID) {
global $wpdb;
$catigoria = get_the_title($post_ID);
$theterm = term_exists($catigoria, \'byartist\');
wp_delete_term($theterm->term_id, \'byartist\');
}
add_action(\'trashed_artists\', \'delete_artist_term_automatically\');
更新:在获得帖子名称而非标题后,我现在使用slug的get\\u term\\u。但这似乎不太管用。我不确定这是否是获取term\\u id的正确方法。我还删除了$wpdb。
function delete_artist_term_automatically($post_ID) {
$cat = get_post($post_ID);
$termslug = $cat->post_name;
$theterm = get_term_by(\'slug\', $termslug, \'byartist\');
wp_delete_term($theterm->term_id, \'byartist\');
}
add_action(\'deleted_artists\', \'delete_artist_term_automatically\');