wordpress - 以编程方式删除子类别和分配的帖子
问题描述
我有一个问题,无法得到那个排序。我所做的是以编程方式为父类别创建子类别,并通过处理 csv 文件以编程方式为子类别创建帖子:
- 父类别(空,没有帖子,应该保留)
- -Sub 1
- --分1-2
- ---分1-3
- -Sub 2
- --分2-2
- -Sub 3
这工作正常。
我现在要做的是:在再次运行脚本之前删除所有子类别和这些子类别中的所有帖子。只有父类别应该保留,其他所有内容都应该消失。
这在使用 $wpdb 或直接 sql 语句或任何 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 );
}
}
推荐阅读
- outlook - SharePoint 日历使用特定筛选器视图连接到 Outlook(审批工作流)
- sql - 根据组合的其他 2 列查询表中的 1 列
- quarkus - Quarkus - Optaplanner 调整算法
- javascript - Ember 3.25 BelongsTo not sideloading
- arrays - 数组维度必须与 Matlab 中的二进制数组运算匹配
- php - 在 wordpress 中的模板页面中不显示更改?
- javascript - 如何在页面加载之前创建欢迎动画屏幕?
- css - CSS文件中没有任何src引用的字体如何下载
- cypress - 使用 cypress 进行测试时的最佳实践是什么?
- python - 给定列名列表,将包含列表的多列合并为一列