unregister_post_type()
云策文档标注
概述
unregister_post_type() 函数用于注销一个已注册的自定义文章类型,但不可用于内置文章类型。它返回 true 表示成功,或 WP_Error 表示失败。
关键要点
- 函数参数:$post_type(字符串,必需),指定要注销的文章类型。
- 返回值:成功时返回 true,失败或文章类型不存在时返回 WP_Error。
- 限制:无法注销内置文章类型(如 post 或 page),否则会返回 WP_Error。
- 内部操作:注销时会移除文章类型的支持功能、重写规则、元框、钩子和关联分类法,并从全局 $wp_post_types 中删除。
- 钩子:注销后触发 unregistered_post_type 动作钩子。
代码示例
// 简单注销自定义文章类型示例
if( !function_exists( 'plugin_prefix_unregister_post_type' ) ) {
function plugin_prefix_unregister_post_type(){
unregister_post_type( 'project' );
}
}
add_action('init','plugin_prefix_unregister_post_type');注意事项
- 注销前应使用 post_type_exists() 检查文章类型是否存在,以避免错误。
- 注销操作通常在 init 钩子中执行,以确保文章类型已注册。
- 注销不会自动删除该文章类型的现有文章或刷新重写规则,需手动处理。
原文内容
Unregisters a post type.
Description
Cannot be used to unregister built-in post types.
Parameters
$post_typestringrequired-
Post type to unregister.
Source
function unregister_post_type( $post_type ) {
global $wp_post_types;
if ( ! post_type_exists( $post_type ) ) {
return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) );
}
$post_type_object = get_post_type_object( $post_type );
// Do not allow unregistering internal post types.
if ( $post_type_object->_builtin ) {
return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) );
}
$post_type_object->remove_supports();
$post_type_object->remove_rewrite_rules();
$post_type_object->unregister_meta_boxes();
$post_type_object->remove_hooks();
$post_type_object->unregister_taxonomies();
unset( $wp_post_types[ $post_type ] );
/**
* Fires after a post type was unregistered.
*
* @since 4.5.0
*
* @param string $post_type Post type key.
*/
do_action( 'unregistered_post_type', $post_type );
return true;
}
Hooks
- do_action( ‘unregistered_post_type’, string $post_type )
-
Fires after a post type was unregistered.
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |
Skip to note 5 content
Mehadi Hasan Juber
Unregister the Custom Post Type simple example:
if( !function_exists( 'plugin_prefix_unregister_post_type' ) ) { function plugin_prefix_unregister_post_type(){ unregister_post_type( 'project' ); } } add_action('init','plugin_prefix_unregister_post_type');Skip to note 6 content
Muhammad Saqib Sarwar
Disable announcement CPT if it is not main site.
function disable_announcement_cpt_for_subsite(): void { if ( ! is_main_site() ) { unregister_post_type( 'announcement' ); } } add_action( 'init', 'disable_announcement_cpt_for_subsite' );Skip to note 7 content
Alen Joseph
Unregister or Disable One or More Custom Post Types with Clean and Optimized Code
<br />
function disable_oraiste_custom_post_types() {<br />
$post_types = [‘clients’, ‘portfolio’, ‘team’, ‘testimonial’];</p>
<p> foreach ($post_types as $post_type) {<br />
if (post_type_exists($post_type)) {<br />
unregister_post_type($post_type);<br />
}<br />
}<br />
}<br />
add_action(‘init’, ‘disable_oraiste_custom_post_types’, 20);<br />
Skip to note 8 content
vee
From WordPress Uninstall Method document ( https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods/ ), they recommend you to remove data (including options, tables) from DB.
So, unregister custom post type should be called on uninstall method.
Here is example:
‘cpt_customposttype’,
‘numberposts’ => -1,
];
$posts = new WP_Query($args);
while ($posts->have_posts()) {
$posts->the_post();
wp_delete_post(get_the_ID() , true);
}
// unregister custom post type
unregister_post_type(‘cpt_customposttype’);
// flush rewrite rules.
flush_rewrite_rules() ;
}
register_uninstall_hook(__FILE__, ‘cpt_uninstall’);
?>
By default, `unregister_post_type() ` will not flush rewrite rules, will not delete existing posts of its type.
The example above do it all.