Deregister custom post types

时间:2010-11-09 作者:anu

有人知道取消自定义帖子类型注册的方法吗?

是否有等同于register_post_type()?

5 个回复
最合适的回答,由SO网友:Dhinju Divakaran 整理而成

从WordPress 4.5开始,就有了这样的功能,unregister_post_type. 例如:-

function delete_post_type(){
    unregister_post_type( \'blocks\' );
}
add_action(\'init\',\'delete_post_type\');

SO网友:t31os

目前没有用于注销post类型的函数,但过程非常简单。

Andrew Nacin在trac上提供了一些代码,found here 并张贴在下面。

if ( ! function_exists( \'unregister_post_type\' ) ) :
function unregister_post_type( $post_type ) {
    global $wp_post_types;
    if ( isset( $wp_post_types[ $post_type ] ) ) {
        unset( $wp_post_types[ $post_type ] );
        return true;
    }
    return false;
}
endif;
注销内置帖子类型将对WordPress产生未知影响,因此请自行承担风险。注销自定义post类型应该是完全安全的,但自然不会对安装进行清理(即,注销post类型并不等同于从数据库中删除数据)。

我可以想象一些可能需要这样做的场景,但更明智的方法(如果可能的话)是,如果不需要,首先不注册post类型。

SO网友:Vayu

这对我很有用,就像Rarst用remove_action() 如果可能的话。

add_action( \'after_setup_theme\',\'remove_foundation_options\', 100 );

function remove_foundation_options() {   
    remove_action( \'init\', \'Orbit\');    
}

SO网友:Rarst

正如t31os所指出的,很容易从全局变量中删除post类型。

但如果您指的是非核心post类型,那么最好查找注册它的代码并使用remove_action() (如果是不错的代码,那么应该挂接它,而不是直接运行)。

SO网友:Nipun Tyagi

在WordPress版本4.5及以上版本中,它们提供了一个删除post类型的功能(unregister\\u post\\u type)。Example

function delete_post_type(){
unregister_post_type( \'jobs\' );
}
add_action(\'init\',\'delete_post_type\');
这肯定会奏效的。

结束

相关推荐