Removed "Uncategorized" on WordPress Posts With Another Category

Updated 10/30/2024, Posted 07/31/2024 by James Parsons James Parsons 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 automatically 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

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.