unregister_post_type()WP 4.5.0

Unregisters (deletes) a post type.

Can not be used to unregister built-in post types: posts, pages, attachments etc — all those who have _builtin parameter when was registered with register_post_type.

When unregistering a post type, everything associated with it is also removed: rewrite rules, meta boxes, hooks, taxonomies, etc.

To unregister a taxonomy, use unregister_taxonomy().

Hooks from the function

Return

true|WP_Error. True on success, WP_Error on failure or if the post type doesn't exist.

Usage

unregister_post_type( $post_type );
$post_type(string) (required)
Post type to unregister.

Examples

0

#1 Unregister a post type

Suppose that some plugin or theme registers post type product, and we don't need it:

// All post types are registered during ``init`` action,
// so you should unregister them after it.
// Use for that ``wp_loaded`` action or ``init`` but with lower priority.

add_action('init', 'my_unregister_post_type', 999);
function my_unregister_post_type(){
	unregister_post_type('product');
}

Notes

  • Global. Array. $wp_post_types List of post types.

Changelog

Since 4.5.0 Introduced.

unregister_post_type() code WP 6.5.2

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