最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Deleting default category in wordpress

programmeradmin1浏览0评论

I'm looking for a way to delete default category functionality in wordpress but after i delete the default category in wp_terms table in mysql database, it automatically assigns default category to another category.

Does anyone know how to achieve this?

Thank you for any help.

I'm looking for a way to delete default category functionality in wordpress but after i delete the default category in wp_terms table in mysql database, it automatically assigns default category to another category.

Does anyone know how to achieve this?

Thank you for any help.

Share Improve this question asked Jul 11, 2012 at 10:55 Pam ApplePam Apple 1113 silver badges12 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 3

Impossible. WordPress requires a default category, that’s hard coded.

If you need a taxonomy without default value – create a custom taxonomy.

Solution:

This is a very old question but I had a similar requirement and I found a way to do it.

add_filter( 'pre_option_default_category', '__return_empty_string', 999 );

Why the above code will work?

This will give empty string to WordPress when it's trying to set default category on post save here: https://github/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();
    }
}

Later it will try to assign that category here: https://github/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 );
    }

and that function will return true from elseif since category array is not empty but value is empty in here https://github/WordPress/WordPress/blob/7004afe4f4bac1fd17a142051832bdf6be8e6fcf/wp-includes/post.php#L4613-L4622

if ( 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;
}

Note, user will see option to set default category in wp-admin/options-writing.php this code does not disable that but that functionality won't work anymore.

I was able to achieve this by manually removing the record in the database, if I recall correctly it was located in wp_terms but I could be wrong.

The easy way to do it is to go to your table 'wp_options' and set 'default_category' to 0.

I can't understand why people keep saying that a term is an absolute requirement of Wordpress. It's not.

If nothing else, you can change the default category from "uncatogorized" to "news" or something relevant for your use.

发布评论

评论列表(0)

  1. 暂无评论