解决方案:
这是一个非常古老的问题,但我有一个类似的要求,我找到了一种方法。
add_filter( \'pre_option_default_category\', \'__return_empty_string\', 999 );
为什么上面的代码可以工作当WordPress试图在此处保存后设置默认类别时,这将为其提供空字符串:
https://github.com/WordPress/WordPress/blob/7004afe4f4bac1fd17a142051832bdf6be8e6fcf/wp-includes/post.php#L3672-L3680// Make sure we set a valid category.
if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
// \'post\' requires at least one category.
if ( \'post\' == $post_type && \'auto-draft\' != $post_status ) {
$post_category = array( get_option( \'default_category\' ) );
} else {
$post_category = array();
}
}
稍后,它将尝试在此处分配该类别:
https://github.com/WordPress/WordPress/blob/7004afe4f4bac1fd17a142051832bdf6be8e6fcf/wp-includes/post.php#L3955-L3957 if ( is_object_in_taxonomy( $post_type, \'category\' ) ) {
wp_set_post_categories( $post_ID, $post_category );
}
该函数将从elseif返回true,因为类别数组不是空的,但此处的值是空的
https://github.com/WordPress/WordPress/blob/7004afe4f4bac1fd17a142051832bdf6be8e6fcf/wp-includes/post.php#L4613-L4622if ( empty( $post_categories ) ) {
if ( \'post\' == $post_type && \'auto-draft\' != $post_status ) {
$post_categories = array( get_option( \'default_category\' ) );
$append = false;
} else {
$post_categories = array();
}
} elseif ( 1 == count( $post_categories ) && \'\' == reset( $post_categories ) ) {
return true;
}
注意,用户将看到在中设置默认类别的选项
wp-admin/options-writing.php
此代码不会禁用该功能,但该功能将不再工作。