Search code examples
wordpresscustom-wordpress-pages

How to disable Default Post Category in WordPress?


I'm creating a new post without selecting category but whenever new post is created at that time it automatically selects category. I want to disable auto select category.

enter image description here

Can anyone help me?


Solution

  • Every single post created in Wordpress will be assigned to a category by default. Even if you don't create categories, Posts will be assigned to a default category called "Uncategorized". This is how the Wordpress system works.

    First of all, can you explain to us why you don't want to assign categories?

    is this because you don't want the category slug added to the post URL? If this is the reason, there are so few ways to achieve this.

    This plugin removes the CATEGORY slug from the post URL.

    https://wordpress.org/plugins/remove-category-url/

    Few other references to removing Category slug from URL without plugins:

    https://jonnyjordan.com/blog/how-to-remove-category-from-your-urls-in-wordpress/

    Remove category & tag base from WordPress url - without a plugin

    Wordpress post will have 1 CATEGORY. This is how Wordpress works. Maybe if you want to remove the default category but if you want to select a category manually then we can achieve that with some custom coding. But you cannot have a WordPress post without assigning a category to it.

    The below code will remove the default category when another category is set to a post. Make sure you have set the default category as Uncategorized.

    //remove default category (uncategorized) when another category has been set
    function remove_default_category($ID, $post) {
    
        //get all categories for the post
        $categories = wp_get_object_terms($ID, 'category');
    
        //if there is more than one category set, check to see if one of them is the default
        if (count($categories) > 1) { 
            foreach ($categories as $key => $category) {
                //if category is the default, then remove it
                if ($category->name == "Uncategorized") {
                    wp_remove_object_terms($ID, 'uncategorized', 'category');
                }
            }
        }
    }
    //hook in to the publsh_post action to run when a post is published
    add_action('publish_post', 'remove_default_category', 10, 2);
    

    Do let me know if this helps.