Skip to main content

How to Automatically Remove ‘Uncategorized’ from Multi-Categorized Posts in WordPress

This script is designed to efficiently manage and clean up the categorization of posts in a WordPress site. Specifically, it targets posts that are currently assigned to the ‘Uncategorized’ category. The script automatically scans through all such posts and checks if they are also assigned to any other categories.

If a post is found to be in ‘Uncategorized’ as well as in one or more additional categories, the script will remove it from ‘Uncategorized’. This process helps in maintaining a more organized and meaningful categorization of content, ensuring that posts are not left in the default ‘Uncategorized’ category if they have been appropriately categorized elsewhere. This is particularly useful for large WordPress sites, where posts might inadvertently be left in ‘Uncategorized’ during the content creation process.

By running this script, site administrators can ensure a cleaner, more accurate categorization of their posts, enhancing both site navigation and SEO performance.

function remove_uncategorized_from_all_posts() {
    $uncategorized_id = get_cat_ID('uncategorized');

    // Get all posts that are in 'uncategorized' category
    $args = array(
        'category__in'   => array($uncategorized_id),
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'posts_per_page' => -1,
    );

    $posts = get_posts($args);

    foreach ($posts as $post) {
        $categories = wp_get_post_categories($post->ID);

        // If the post has more than one category, remove 'uncategorized'
        if (count($categories) > 1) {
            $categories = array_diff($categories, array($uncategorized_id));
            wp_set_post_categories($post->ID, $categories);
        }
    }
}

// Run the function - you can comment this out after it's run once
// remove_uncategorized_from_all_posts();

How to Use

  1. Backup Your Database: Before running this script, make sure to back up your WordPress database. This script will modify your posts, so it’s important to have a backup in case something goes wrong.
  2. Add the Script: Place the function in your theme’s functions.php file.
  3. Run the Script: Uncomment the call to remove_uncategorized_from_all_posts() and save the file. This will execute the function.
  4. Comment Out or Remove the Script: After the script has run, you should comment out or remove the call to the function to prevent it from running on every page load. This is important because the script is resource-intensive and should only be run once.

Important Notes

  • This script is designed to run once. Running this on every page load would significantly impact your site’s performance.
  • The script uses get_posts with 'posts_per_page' => -1, which means it will attempt to load all matching posts at once. If your site has a very large number of posts, this could lead to memory issues. In such cases, consider processing the posts in batches.
  • Always test scripts like this in a staging environment before running them on a live site to avoid unintended consequences.
  • If you’re not comfortable editing your theme’s files or running this kind of script, you might want to seek assistance from a developer.

Leave a Reply