简体   繁体   中英

Wordpress: How do I remove the category "uncategorized" from all posts in bulk?

Scenario:

I have 1000 posts that have the "Uncategorized" category, and I want to remove "Uncategorized" from all of those posts and set a different category for those posts.

In other words– take all Uncategorized posts and move them to another category, in one fell swoop.

Can I do this in bulk without going through each post individually?

What you are looking for is the WordPress bulk editor.

  1. Go to Posts > Categories > Uncategorized

  2. Click the "Screen Options" tab in the top right corner, then change "Number of items per page:" to 1000. (If you are on a really slow server you might consider doing less at a time)

  3. Now select all of the items on the page and click the "Bulk Actions" drop-down above the select all and select the "Edit” option.

  4. Hit Apply

  5. In the bulk editor click the “new category” you want to change all of the posts to and hit update.

Once you have added all of the posts to the “new category” you need to remove the “Uncategorized” category. To do this:

  1. Go to Settings > Writing
  2. Now change the “Default Post Category” to something besides “Uncategorized”
  3. Go back to Posts > Categories and delete the “Uncategorized” category
  4. Now you can create the “Uncategorized” category again if you like and change it back to the default.

Once you delete the “Uncategorized” category it will remove it from all of your posts.

If you have some posts that you want to remain in “Uncategorized” then create a new category called “temp” and assign all of the posts you want to remain to that category. Once you delete “Uncategorized” create it again and assign the posts in “temp” back to that category.

The uncategorized category has an ID of 1 . What our worksflow will be is,

  • Get all posts which is assigned to the uncategorized category.

  • We will only get post ID's which will make our query up to 1000 times faster on a site with thousands of posts. This will also help that our query does not time out or hit a maximum memory fatal error.

Use wp_set_object_terms() to remove and set our new tems

NOTE:

The code below requires PHP 5.4+ and any changes will be non reversable, so back up your database first

$args = [
    'nopaging' => true, // Gets all posts
    'cat' => 1, // Only gets posts assigned to category 1 which is the uncategorized category
    'fields' => 'ids', // Only get post ID's, make query up 1000 times faster on huge databases
];
$q = get_posts( $args );

if ( $q ) {

    foreach ( $q as $v ) {
        // Get all the post categories
        $categories = get_the_category( $v );
        $category_ids = []; 
        foreach ( $categories as $category ) {
            // Replace all uncategorized category instances with our new category id and build new array
            if ( $category->term_id == 1 ) {
                $category_ids[] = (int) 21; // REPLACE WITH THE CORRECT NEW CATEGORY ID
            } else { 
                $category_ids[] = (int) $category->term_id;
            }
        } 
        // Set our new categories to the post
        if ( $category_ids ) // Unnecessary check for categories, but just in case should something fail
            wp_set_object_terms( $v, $category_ids, 'category' );
    }

}

Note, nowhere have we changed the $post global or setup postdata, so we don't need to call wp_reset_postdata() :-)

As you've discovered, the bulk editor only allows the ADDITION of categories to multiple posts - it's not possible to REMOVE categories from multiple posts. The best option i found was to temporarily install this plug-in https://wordpress.org/plugins/bulk-remove-posts-from-category/ (in the WP repository) which adds the ability to REMOVE categories from multiple posts using the same bulk edit method. it simply adds an additional 'remove' checkbox under the category list.

Wordpress stores the parent/child relationships between categories and posts in the wp_term_relationships table, which is documented here . As @Pieter Goosen noted, the "Uncategoried" category has a ID of 1 . So you can backup your SQL database , then connect to it with a SQL command line client ( sudo mysql wordpress works for me), and run this SQL command:

delete from wp_term_relationships where term_taxonomy_id = 1;

If you want to move all uncategorized posts to another category, you can use the Bulk Move plugin.

If you want to remove a category from some posts, Bulk remove posts from category might be a better option.

As Melebius said, it is much, much quicker (and easier) to use https://wordpress.org/plugins/bulk-remove-posts-from-category/ . It works with products as well as posts. Brilliant!

You can add this to your theme's functions.php file, then refresh any page on your site once, then remove the function.

Use at your own risk and backup the database first! There's no UNDO button on this.

<?php
$args = array(
    'posts_per_page' => -1
);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
    setup_postdata( $post );

    $categories = get_the_category();
    $catcount = count($categories);
    $postid = $post->ID;

    $catlist = array();

    //Building a list of categories for each post and EXCLUDING "uncategorized"

    foreach( $categories as $category ) {
        if($category->name == 'Uncategorized') {
            continue;
        }

        $catlist[] = $category->term_id;
    }

    // If there's just one category, and that category is "Uncategorized", move the category to one of your choosing
    if($catcount == 1 && $categories[0]->name == "Uncategorized") {
        // This is the category ID that you want to move uncategorized posts to
        $catlist = array(189);
    }

    wp_set_object_terms( $postid, $catlist, 'category' );


endforeach; 
wp_reset_postdata(); 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM