categories - How do you bulk remove a category from posts?

admin2025-06-02  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.

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.

Share Improve this question asked Jan 16, 2016 at 0:27 CoreyCorey 3217 silver badges17 bronze badges 2
  • assign a different one as a default and delete it? – Mark Kaplun 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
Add a comment  | 

2 Answers 2

Reset to default 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

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;
}   
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748824680a314030.html

最新回复(0)