How do I remove a taxonomy from Wordpress?

不打扰是莪最后的温柔 提交于 2019-12-03 05:39:14

问题


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:


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/




回答5:


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');
    });



回答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'); }



来源:https://stackoverflow.com/questions/4249694/how-do-i-remove-a-taxonomy-from-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!