首页 > 解决方案 > 以编程方式删除子类别和分配的帖子

问题描述

我有一个问题,无法得到那个排序。我所做的是以编程方式为父类别创建子类别,并通过处理 csv 文件以编程方式为子类别创建帖子:

这工作正常。

我现在要做的是:在再次运行脚本之前删除所有子类别和这些子类别中的所有帖子。只有父类别应该保留,其他所有内容都应该消失。

这在使用 $wpdb 或直接 sql 语句或任何 wordpress 函数的单个查询中是否可行?

谢谢你和亲切的问候,汤姆

标签: wordpress

解决方案


听起来很愚蠢,...但是通过提出问题,我找到了解决方案:

function delete_posts_title($partner) {

//delete all posts
$catposts = get_posts( array(
    'category'       => $partner['kategorie_id'],
    'numberposts'    => -1
) );
$i = 0;
foreach($catposts as $post) {
    //exclude posts that are in these categories, too
    if(!in_category( array(1,4145),$post->ID)) {
        //echo "<pre>".$post->post_title." ". $i ."</pre>";
        wp_delete_post( $post->ID, false );
        ++$i;
    }
}

//delete all subcategories
$args = array('child_of' => $partner['kategorie_id']);
$categories = get_categories( $args );

foreach($categories as $category) { 
    wp_delete_category( $category->term_id );
}

}


推荐阅读