最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to delete all categories programatically?

programmeradmin0浏览0评论

Simple questions - how do I delete all categories programatically?

For instance this returns a list of all categories

$args = array(  
    "hide_empty" => 0,
    "type"       => "post",
    "orderby"    => "name",
    "order"      => "ASC" 
);
$types = get_categories($args);

How do I simply delete them so I can replace them with other categories?

Simple questions - how do I delete all categories programatically?

For instance this returns a list of all categories

$args = array(  
    "hide_empty" => 0,
    "type"       => "post",
    "orderby"    => "name",
    "order"      => "ASC" 
);
$types = get_categories($args);

How do I simply delete them so I can replace them with other categories?

Share Improve this question edited Jun 27, 2017 at 9:08 CodeMascot 4,5372 gold badges15 silver badges25 bronze badges asked Jun 27, 2017 at 8:25 user122667user122667 32 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Please have look on the below code block-

$args = array(
    "hide_empty" => 0,
    "type"       => "post",
    "orderby"    => "name",
    "order"      => "ASC"
);
$types = get_categories($args);

foreach ( $types as $type) {
    wp_delete_category( $type->ID );
}

The function wp_delete_category will delete a single category. So we need to run a loop through $types to delete each single category.

Hope that helps.

$cats = get_categories( [
    'hide_empty'   => 0
] );
foreach( $cats as $cat ) {
    wp_delete_category( $cat->term_id );
}
发布评论

评论列表(0)

  1. 暂无评论