Deleting default category in wordpress

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?

Read More

Thank you for any help.

Related posts

Leave a Reply

5 comments

  1. 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.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();
        }
    }
    

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

    and that function will return true from elseif since category array is not empty but value is empty in here https://github.com/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.

  2. 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.

  3. 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.