我正在尝试将一个类别添加到应用了自定义税务类别的帖子中。如果帖子有自定义税类别,那么它还应该得到一个标准类别。
因此,下面的代码将添加标准类别,但它会将其添加到保存的每个帖子中,而不仅仅是那些具有自定义税务类别的帖子。我做错了什么?
add_action( \'save_post\', \'set_new_category\' );
function set_new_category( $post_id ) {
//Define new category by ID
$new_category = 27;
//Define the custom taxonomy
$em_taxonomy = \'event-category\';
// Get the custom taxonomy category by slug
$old_term = wp_get_post_terms( $post_id, \'building-reservations\', $em_taxonomy );
// Check if post has custom taxonomy category applied
if (!$old_term)
//If not, don\'t do anything
return;
//Append with the new category
wp_set_post_categories( $post_id, $new_category, $append = true );
}