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.

Batch script to update WooCommerce product titles

I had a client who had named all of his 600+ products to uppercase and Google Shopping was not allowing the products to be indexed because of this.
To automatically rename the Woocommerce Product titles, I ran this script:

This will loop through each product and rename the titles to capitalised using php UCWORDS()

For example: MY PRODUCT will be renamed to My Product.

Read More

Add featured image to admin table in WordPress for custom post type

On a project I’m doing, I created a custom post type for a catalogue and want the featured image to display on the admin table.

My custom post type is called “product” and I want to limit the feature to only the “product” custom post type. Here’s how I did it.

I added this into functions.php

Read More

Remove container and UL on WordPress nav menu

Sometimes you might want to programatically include other things in your menu in WordPress and to do that you will want to control the UL tags so you can insert other LI items within it.
The code below will allow you to spit out ONLY the LI tags from a WordPress Nav Menu, you will have to wrap it in UL yourself so you can add extra LI.

Read More