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

问题描述 投票:2回答:4

场景:

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

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

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

wordpress categories
4个回答
9
投票

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

  1. 转到帖子>类别>未分类
  2. 单击右上角的“屏幕选项”选项卡,然后将“每页项目数:”更改为1000.(如果您使用的是非常慢的服务器,则可以考虑一次减少)
  3. 现在选择页面上的所有项目,然后单击全选上方的“批量操作”下拉列表,然后选择“编辑”选项。
  4. 点击申请
  5. 在批量编辑器中,单击要更改所有帖子的“新类别”并点击更新。

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

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

删除“未分类”类别后,它会将其从您的所有帖子中删除。

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


5
投票

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

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

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

注意:

下面的代码需要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() :-)


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;

0
投票

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


-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(); 
?>
© www.soinside.com 2019 - 2024. All rights reserved.