0

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.

4

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

1

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;
}   
  • 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 Jan 16 '16 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 Jan 16 '16 at 8:09

protected by Community Mar 5 at 16:33

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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