1

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
  • assign a different one as a default and delete it? Commented Jan 16, 2016 at 5:08
  • use Bulk Move plugin, Easy fast and secure. Url of bulk move
    – David Corp
    Commented Oct 9, 2017 at 0:40

2 Answers 2

5

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

2

I already have the same problem. So i write the function to remove uncategorized for all post.

You can embed this function to plugins or themes and call it.


function deleteUnCategoryForAllPost(){  
    global $wpdb;
    $result =  $wpdb->query( $wpdb->prepare("delete FROM $wpdb->term_relationships where term_taxonomy_id =1 ") );
    return $result;
}   
2
  • Hello, i think use custom query or not It depend on your purpose. When i import my database from other. I often use this way. It run faster and work as expected.
    – Huy Nguyen
    Commented Jan 16, 2016 at 7:45
  • You should make the term_taxonomy_id dynamic, ie a parameter of the function. Besides that, a custom query is in this certainly faster than hundreds of queries per API.
    – fuxia
    Commented Jan 16, 2016 at 8:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.