How do I remove a taxonomy from Wordpress?

2020-05-19 05:13发布

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.

7条回答
乱世女痞
2楼-- · 2020-05-19 05:26

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

function unregister_taxonomy(){
    register_taxonomy('post_tag', array());
}
add_action('admin_init', 'unregister_taxonomy');
查看更多
3楼-- · 2020-05-19 05:27

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

查看更多
别忘想泡老子
4楼-- · 2020-05-19 05:31

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' );
}
查看更多
Animai°情兽
5楼-- · 2020-05-19 05:37

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]);
}
查看更多
疯言疯语
6楼-- · 2020-05-19 05:38

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');
    });
查看更多
迷人小祖宗
7楼-- · 2020-05-19 05:43

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' );
查看更多
登录 后发表回答