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?

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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