How do I remove a taxonomy from Wordpress?

2020-05-19 05:35发布

问题:

I'm creating different custom post types and taxonomies and I want to remove the 'Post Tags' taxonomy from the default 'Posts' post type. How do I go about doing this?

Thanks.

回答1:

I suggest you don't mess with the actual global. Its safer to simply deregister the taxonomy from the post type: register_taxonomy is used for both creation and modification.

function ev_unregister_taxonomy(){
    register_taxonomy('post_tag', array());
}
add_action('init', 'ev_unregister_taxonomy');

To remove the sidebar menu entry:

// Remove menu
function remove_menus(){
    remove_menu_page('edit-tags.php?taxonomy=post_tag'); // Post tags
}

add_action( 'admin_menu', 'remove_menus' );


回答2:

Perhaps a more technically correct method would be to use unregister_taxonomy_for_object_type

add_action( 'init', 'unregister_tags' );

function unregister_tags() {
    unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}


回答3:

Where it says 'taxonomy_to_remove' is where you'll enter the taxonomy you want to remove. For instance you can replace it with the existing, post_tag or category.

add_action( 'init', 'unregister_taxonomy');
function unregister_taxonomy(){
    global $wp_taxonomies;
    $taxonomy = 'taxonomy_to_remove';
    if ( taxonomy_exists( $taxonomy))
        unset( $wp_taxonomies[$taxonomy]);
}


回答4:

Total unregister and remove (minimal PHP version 5.4!)

add_action('init', function(){
        global $wp_taxonomies;
        unregister_taxonomy_for_object_type( 'category', 'post' );
        unregister_taxonomy_for_object_type( 'post_tag', 'post' );
        if ( taxonomy_exists( 'category'))
            unset( $wp_taxonomies['category']);
        if ( taxonomy_exists( 'post_tag'))
            unset( $wp_taxonomies['post_tag']);
        unregister_taxonomy('category');
        unregister_taxonomy('post_tag');
    });


回答5:

There is new function to remove taxonomy from WordPress.

Use unregister_taxonomy( string $taxonomy ) function

See details: https://developer.wordpress.org/reference/functions/unregister_taxonomy/



回答6:

Use it in 'admin_init' hook insetead not 'init'

function unregister_taxonomy(){
    register_taxonomy('post_tag', array());
}
add_action('admin_init', 'unregister_taxonomy');


回答7:

add_action('admin_menu', 'remove_menu_items'); function remove_menu_items() { remove_submenu_page('edit.php','edit-tags.php?taxonomy=post_tag'); }