我最近遇到了类似的情况。虽然我目前记不清源代码,但下面的代码将为您选择的分类法创建一个新条目,它可以作为满足您需求的基础。
//Automatically creates a category in the case log and adds it to the post.
function add_title_as_category( $postid ) {
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;
$post = get_post($postid);
if ( $post->post_type == \'case\') { // change \'post\' to any cpt you want to target
$term = get_term_by(\'slug\', $post->post_name, \'case-log\');
if ( empty($term) ) {
$add = wp_insert_term( $post->post_title, \'case-log\', array(\'slug\'=> $post->post_name) );
if ( is_array($add) && isset($add[\'term_id\']) ) {
wp_set_object_terms($postid, $add[\'term_id\'], \'case-log\', true );
}
}
}}add\\u action(\'save\\u post\',\'add\\u title\\u as\\u category\');