Removed "Uncategorized" on WordPress Posts With Another Category

James Parsons by James Parsons Updated Jul 31st, 2024 0 Comments

WordPress has an annoying little bug. Occasionally, when you set a category on a post, the old "Uncategorized" category will stay there. This means your post can end up with two categories: the category you just assigned, and the "Uncategorized" one.

I noticed this happens when using the bulk post editor as well. It's easy to add categories to posts in bulk, but WordPress doesn't let you remove them in bulk.

If you have dozens or hundreds of posts, this can be pretty tedious. You don't want to have to go through them one by one just to manually remove the "Uncategorized" category from all of your posts, especially when they already have another category that is properly assigned. They aren't uncategorized anymore. They have a category. Right?

Sigh.

This handy code fixes this bug. It will find all posts that belong to the "Uncategorized" category, and if they also belong to a real category, it will automatimcally remove the "Uncategorized" one.

Here's the code, add this to the bottom of your theme's functions.php file:

// For all posts with two or more categories where one of them is the default "Uncategorized" category, automatically remove the "Uncategorized" category

function remove_uncategorized_category_from_posts() {
  $args = array(
      'posts_per_page' => -1,
      'post_type' => 'post',
      'post_status' => 'any',
  );
  $posts = get_posts($args);
  foreach ($posts as $post) {
      $categories = wp_get_post_categories($post->ID);
      if (count($categories) > 1) {
          $uncategorized_id = get_cat_ID('Uncategorized');
          if (($key = array_search($uncategorized_id, $categories)) !== false) {
              unset($categories[$key]);
              wp_set_post_categories($post->ID, $categories);
          }
      }
  }
}
add_action('load-edit.php', 'remove_uncategorized_category_from_posts');

Now, you can bulk assign categories to posts that are "Uncategorized", and those posts won't end up with two categories; they'll just have one.

"Uncategorized" posts are now only posts that have not been assigned another category yet. That's how it should have been from the start, right?

Did this help you? Do you have questions for me? Please let me know in the comments below!

Related Code Snippets

Written by James Parsons

James Parsons is the founder and CEO of Content Powered, a premier content marketing agency that leverages nearly two decades of his experience in content marketing to drive business growth. Renowned for founding and scaling multi-million dollar eCommerce businesses through strategic content marketing, James has become a trusted voice in the industry, sharing his insights in Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, Inc, and other leading publications. His background encompasses key roles across various agencies, contributing to the content strategies of major brands like eBay and Expedia. James's expertise spans SEO, conversion rate optimization, and effective content strategies, making him a pivotal figure in the industry.