批量删除某些分类法的所有术语(Wordpress)

Bulk delete all terms of certain taxonomy (Wordpress)

我有删除分类法的代码。这段代码可以工作,我只是不知道将它粘贴到哪里才能工作。在 wordpress 或 phpmyadmin 中的某处,或哪里?

    $taxonomy_name = 'city';
    $terms = get_terms( array(
        'taxonomy' => $taxonomy_name,
        'hide_empty' => false
    ) );
    foreach ( $terms as $term ) {
               wp_delete_term($term->term_id, $taxonomy_name); 
        }        
}
add_action( 'wp_head', 'delete_all_terms' );

这应该粘贴到 functions.php (https://www.wpbeginner.com/glossary/functions-php/)。

但是您的代码缺少一个函数:

function delete_all_terms(){
    $taxonomy_name = 'city';
    $terms = get_terms( array(
        'taxonomy' => $taxonomy_name,
        'hide_empty' => false
    ) );
    foreach ( $terms as $term ) {
               wp_delete_term($term->term_id, $taxonomy_name); 
        }        
}
add_action( 'wp_head', 'delete_all_terms' );

另外请确保之前备份您的 Wordpress 数据库。