I just imported a Blogger site to WordPress, and it put the category “Uncategorized” on all posts. I ended up converting all of the tags (which did come over from Blogger) to categories, but now I still have “Uncategorized” also set for all of my posts.

I’m trying to find out how to remove the “Uncategorized” category from all of my posts.

2 Answers
2

You can make use of wp_remove_object_terms() to remove the desired category from a post.

What we will do is, run a custom query to get all the post with the associated category, and then loop through the posts and use wp_remove_object_terms() to remove the category

FEW NOTES:

  • The code is untested and might be buggy. Be sure to test this locally first

  • If you have a huge amount of posts, this might lead to a fatal error due to timing out. To avoid this, run the function a couple of times with a smaller amount of posts till the whole operation is complete

THE CODE:

add_action( 'init', function()
{
    // Get all the posts which is assigned to the uncategorized category
    $args = [
        'posts_per_page' => -1, // Adjust as needed
        'cat'            => 1, // Category ID for uncategorized category
        'fields'         => 'ids', // Only get post ID's for performance
        // Add any additional args here, see WP_Query
    ];
    $q = get_posts( $args );

    // Make sure we have posts
    if ( !$q )
        return;

    // We have posts, lets loop through them and remove the category
    foreach ( $q as $id )
        wp_remove_object_terms(
            $id, // Post ID
            1, // Term ID to remove
            'category' // The taxonomy the term belongs to
        );
}, PHP_INT_MAX );

You can just simply drop this into your functions file and then load any page back end or front end. You can then remove the code from your functions file

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *