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?

2 Answers 2

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.