Wordpress:如何从批量的所有帖子中删除“未分类”类别?

时间:2015-07-22 16:52:47

标签: wordpress categories

情景:

我有1000个帖子有“未分类”类别,我想从所有帖子中删除“未分类”,并为这些帖子设置不同的类别。

换句话说 - 将所有未分类的帖子转移到另一个类别,一下子。

我是否可以批量执行此操作而无需单独浏览每个帖子?

6 个答案:

答案 0 :(得分:7)

您正在寻找的是WordPress批量编辑器。

  1. 转到帖子>分类>未归类

  2. 点击"屏幕选项"右上角的标签,然后更改"每页的项目数:"到1000.(如果你的服务器很慢,你可能会考虑一次少做)

  3. 现在选择页面上的所有项目,然后点击"批量操作"在选择全部上方下拉,然后选择"编辑“选项。

  4. 点击申请

  5. 在批量编辑器中,点击要更改所有帖子的“新类别”并点击更新。

  6. 将所有帖子添加到“新类别”后,您需要删除“未分类”类别。要做到这一点:

    1. 转到设置>写作
    2. 现在将“默认帖子类别”更改为“未分类”
    3. 之外的内容
    4. 返回帖子>分类并删除“未分类”类别
    5. 现在,如果您愿意,可以再次创建“未分类”类别,并将其更改回默认值。
    6. 删除“未分类”类别后,它会将其从您的所有帖子中删除。

      如果您希望某些帖子保留在“未分类”中,则创建一个名为“temp”的新类别,并将您要保留的所有帖子分配到该类别。删除“Uncategorized”后再次创建它并将“temp”中的帖子分配回该类别。

答案 1 :(得分:5)

未分类类别的ID为1。我们的工作流程是什么,

  • 获取分配到未分类类别的所有帖子。

  • 我们只会获得帖子ID,这样可以在拥有数千个帖子的网站上将查询速度提高1000倍。这也有助于我们的查询不会超时或达到最大内存致命错误。

使用wp_set_object_terms()删除并设置我们的新标准

注意:

以下代码需要PHP 5.4+并且任何更改都是不可撤消的,因此请先备份数据库

$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' );
    }

}

注意,我们没有更改$post全局或设置postdata,因此我们无需致电wp_reset_postdata(): - )

答案 2 :(得分:0)

Wordpress将wp_term_relationships表中存储的类别和帖子之间的父子关系存储在here中。如@Pieter Goosen所述,“未分类”类别的ID为1。因此,您可以备份您的SQL数据库,然后使用SQL命令行客户端(sudo mysql wordpress对我有用)连接到该数据库,然后运行以下SQL命令:

delete from wp_term_relationships where term_taxonomy_id = 1;

答案 3 :(得分:0)

您已经发现,批量编辑器仅允许将类别添加到多个帖子-无法从多个帖子中删除类别。我发现最好的选择是临时安装此插件https://wordpress.org/plugins/bulk-remove-posts-from-category/(在WP存储库中),该插件使用相同的批量编辑方法添加了从多个帖子中删除类别的功能。它只是在类别列表下添加了一个额外的“删除”复选框。

答案 4 :(得分:0)

如果您想将所有未分类的帖子移动到另一个类别,您可以使用 Bulk Move 插件。

如果您想从一些帖子中删除某个类别,Bulk remove posts from category 可能是更好的选择。

答案 5 :(得分:-1)

您可以将其添加到主题的functions.php文件中,然后刷新您网站上的任何页面,然后删除该功能。

使用风险并首先备份数据库!这上面没有UNDO按钮。

<?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(); 
?>