unregister_post_type

函数
unregister_post_type ( $post_type )
参数
  • (string) $post_type Post type to unregister.
    Required:
返回值
  • (true|WP_Error) True on success, WP_Error on failure or if the post type doesn't exist.
定义位置
相关方法
register_post_typeunregister_post_metaunregister_block_typeregister_post_metaregister_block_type
引入
4.5.0
弃用
-

unregister_post_type: 这个函数取消了以前用 register_post_type 函数注册的自定义文章类型。一旦取消注册,该文章类型和它的所有数据将从数据库中删除。

取消注册一个文章类型。

不能用于取消注册内置的文章类型。

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

常见问题

FAQs
查看更多 >