Is there a way to ‘Lock’ a Taxonomy?

Categories, Tags & Taxonomies

First i want to make this one clear: Everything is a Taxonomy. Tags is a non-hierarchical, Categories a hierarchical and both are built-in taxonomies. Same goes for eg. post formats (aside, chat, etc.). It’s the same concept as with post-types (post, page, attachment, nav_menu_item, etc. are all just built in post types).

Everything inside one of those Taxonomies is a Term. For eg. (inside post formats) “aside”, “quote”, “audio”.

Codex Links

The Concept

The following is for your functions.php file. This triggers on every page request. You could improve it – using the Transients API – to trigger on given timestamps (eg. twice daily, hourly, etc.).

function wpse14350_cleanup_taxonomy_terms()
{
    // Define your terms inside this array
    $predefined_terms = array(
         'term A'
        ,'term B'
        ,'term C'
    );
    // Name your taxonomy here
    $predefined_taxonomy = 'category';

    $all_terms_inside_tax = get_terms( 
         $predefined_taxonomy
        ,array(
             'hide_empty'   => false
            ,'taxonomy'     => $predefined_taxonomy
        ) 
    );

    foreach ( $all_terms_inside_tax as $term )
    {
        if ( ! in_array( $term->name, $predefined_terms ) )
            wp_delete_term( $term->term_id, $predefined_taxonomy );
    }
}
add_action( 'init', 'wpse14350_cleanup_taxonomy_terms' );

Leave a Comment