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 badges2 Answers
Reset to default 2Please 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 );
}