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.
* @param string $post_type Post type key.
do_action( 'unregistered_post_type', $post_type );
* Build an object with all post type capabilities out of a post type object
* Post type capabilities use the 'capability_type' argument as a base, if the
* capability is not set in the 'capabilities' argument array or if the
* 'capabilities' argument is not supplied.
* The capability_type argument can optionally be registered as an array, with
* the first value being singular and the second plural, e.g. array('story, 'stories')
* Otherwise, an 's' will be added to the value for the plural form. After
* registration, capability_type will always be a string of the singular value.
* By default, eight keys are accepted as part of the capabilities array:
* - edit_post, read_post, and delete_post are meta capabilities, which are then
* generally mapped to corresponding primitive capabilities depending on the
* context, which would be the post being edited/read/deleted and the user or
* role being checked. Thus these capabilities would generally not be granted
* directly to users or roles.
* - edit_posts - Controls whether objects of this post type can be edited.
* - edit_others_posts - Controls whether objects of this type owned by other users
* can be edited. If the post type does not support an author, then this will
* behave like edit_posts.
* - delete_posts - Controls whether objects of this post type can be deleted.
* - publish_posts - Controls publishing objects of this post type.
* - read_private_posts - Controls whether private objects can be read.
* These five primitive capabilities are checked in core in various locations.
* There are also six other primitive capabilities which are not referenced
* directly in core, except in map_meta_cap(), which takes the three aforementioned
* meta capabilities and translates them into one or more primitive capabilities
* that must then be checked against the user or role, depending on the context.
* - read - Controls whether objects of this post type can be read.
* - delete_private_posts - Controls whether private objects can be deleted.
* - delete_published_posts - Controls whether published objects can be deleted.
* - delete_others_posts - Controls whether objects owned by other users can be
* can be deleted. If the post type does not support an author, then this will
* behave like delete_posts.
* - edit_private_posts - Controls whether private objects can be edited.
* - edit_published_posts - Controls whether published objects can be edited.
* These additional capabilities are only used in map_meta_cap(). Thus, they are
* only assigned by default if the post type is registered with the 'map_meta_cap'
* argument set to true (default is false).
* @since 5.4.0 'delete_posts' is included in default capabilities.
* @see register_post_type()
* @param object $args Post type registration arguments.
* @return object Object with all the capabilities as member variables.
function get_post_type_capabilities( $args ) {
if ( ! is_array( $args->capability_type ) ) {
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
// Singular base for meta capabilities, plural base for primitive capabilities.
list( $singular_base, $plural_base ) = $args->capability_type;
$default_capabilities = array(
'edit_post' => 'edit_' . $singular_base,
'read_post' => 'read_' . $singular_base,
'delete_post' => 'delete_' . $singular_base,
// Primitive capabilities used outside of map_meta_cap():
'edit_posts' => 'edit_' . $plural_base,
'edit_others_posts' => 'edit_others_' . $plural_base,
'delete_posts' => 'delete_' . $plural_base,
'publish_posts' => 'publish_' . $plural_base,
'read_private_posts' => 'read_private_' . $plural_base,
// Primitive capabilities used within map_meta_cap():
if ( $args->map_meta_cap ) {
$default_capabilities_for_mapping = array(
'delete_private_posts' => 'delete_private_' . $plural_base,
'delete_published_posts' => 'delete_published_' . $plural_base,
'delete_others_posts' => 'delete_others_' . $plural_base,
'edit_private_posts' => 'edit_private_' . $plural_base,
'edit_published_posts' => 'edit_published_' . $plural_base,
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
$capabilities = array_merge( $default_capabilities, $args->capabilities );
// Post creation capability simply maps to edit_posts by default:
if ( ! isset( $capabilities['create_posts'] ) ) {
$capabilities['create_posts'] = $capabilities['edit_posts'];
// Remember meta capabilities for future reference.
if ( $args->map_meta_cap ) {
_post_type_meta_capabilities( $capabilities );
return (object) $capabilities;
* Store or return a list of post type meta caps for map_meta_cap().
* @global array $post_type_meta_caps Used to store meta capabilities.
* @param string[] $capabilities Post type meta capabilities.
function _post_type_meta_capabilities( $capabilities = null ) {
global $post_type_meta_caps;
foreach ( $capabilities as $core => $custom ) {
if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ), true ) ) {
$post_type_meta_caps[ $custom ] = $core;
* Builds an object with all post type labels out of a post type object.
* Accepted keys of the label array in the post type object:
* - `name` - General name for the post type, usually plural. The same and overridden
* by `$post_type_object->label`. Default is 'Posts' / 'Pages'.
* - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'.
* - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types.
* When internationalizing this string, please use a {@link https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#disambiguation-by-context gettext context}
* matching your post type. Example: `_x( 'Add New', 'product', 'textdomain' );`.
* - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'.
* - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'.
* - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'.
* - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'.
* - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'.
* - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'.
* - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'.
* - `not_found_in_trash` - Label used when no items are in the Trash. Default is 'No posts found in Trash' /
* 'No pages found in Trash'.
* - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical
* post types. Default is 'Parent Page:'.
* - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'.
* - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'.
* - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'.
* - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'.
* - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' /
* 'Uploaded to this page'.
* - `featured_image` - Label for the featured image meta box title. Default is 'Featured image'.
* - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'.
* - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'.
* - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'.
* - `menu_name` - Label for the menu name. Default is the same as `name`.
* - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' /
* - `filter_by_date` - Label for the date filter in list tables. Default is 'Filter by date'.
* - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' /
* 'Pages list navigation'.
* - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'.
* - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.'
* - `item_published_privately` - Label used when an item is published with private visibility.
* Default is 'Post published privately.' / 'Page published privately.'
* - `item_reverted_to_draft` - Label used when an item is switched to a draft.
* Default is 'Post reverted to draft.' / 'Page reverted to draft.'
* - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' /
* - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.'
* Above, the first default value is for non-hierarchical post types (like posts)
* and the second one is for hierarchical post types (like pages).
* Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter.
* @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`,
* and `use_featured_image` labels.
* @since 4.4.0 Added the `archives`, `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`,
* `items_list_navigation`, and `items_list` labels.
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
* @since 4.7.0 Added the `view_items` and `attributes` labels.
* @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`,
* `item_scheduled`, and `item_updated` labels.
* @since 5.7.0 Added the `filter_by_date` label.
* @param object|WP_Post_Type $post_type_object Post type object.
* @return object Object with all the labels as member variables.
function get_post_type_labels( $post_type_object ) {
$nohier_vs_hier_defaults = array(
'name' => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
'singular_name' => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
'add_new' => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
'add_new_item' => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
'edit_item' => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
'new_item' => array( __( 'New Post' ), __( 'New Page' ) ),
'view_item' => array( __( 'View Post' ), __( 'View Page' ) ),
'view_items' => array( __( 'View Posts' ), __( 'View Pages' ) ),
'search_items' => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
'not_found' => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
'not_found_in_trash' => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
'parent_item_colon' => array( null, __( 'Parent Page:' ) ),
'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ),
'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
'featured_image' => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
'set_featured_image' => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
'remove_featured_image' => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
'use_featured_image' => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
'filter_by_date' => array( __( 'Filter by date' ), __( 'Filter by date' ) ),
'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ),
'item_published' => array( __( 'Post published.' ), __( 'Page published.' ) ),
'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
'item_reverted_to_draft' => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
'item_scheduled' => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
'item_updated' => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
$post_type = $post_type_object->name;
$default_labels = clone $labels;
* Filters the labels of a specific post type.
* The dynamic portion of the hook name, `$post_type`, refers to
* @see get_post_type_labels() for the full list of labels.
* @param object $labels Object with labels for the post type as member variables.
$labels = apply_filters( "post_type_labels_{$post_type}", $labels );
// Ensure that the filtered labels contain all required default values.
$labels = (object) array_merge( (array) $default_labels, (array) $labels );
* Build an object with custom-something object (post type, taxonomy) labels
* out of a custom-something object
* @param object $object A custom-something object.
* @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels.
* @return object Object containing labels for the given custom-something object.
function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
$object->labels = (array) $object->labels;
if ( isset( $object->label ) && empty( $object->labels['name'] ) ) {
$object->labels['name'] = $object->label;
if ( ! isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) {
$object->labels['singular_name'] = $object->labels['name'];
if ( ! isset( $object->labels['name_admin_bar'] ) ) {
$object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name;
if ( ! isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) {
$object->labels['menu_name'] = $object->labels['name'];
if ( ! isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) {
$object->labels['all_items'] = $object->labels['menu_name'];
if ( ! isset( $object->labels['archives'] ) && isset( $object->labels['all_items'] ) ) {
$object->labels['archives'] = $object->labels['all_items'];
foreach ( $nohier_vs_hier_defaults as $key => $value ) {
$defaults[ $key ] = $object->hierarchical ? $value[1] : $value[0];
$labels = array_merge( $defaults, $object->labels );
$object->labels = (object) $object->labels;
* Add submenus for post types.
function _add_post_type_submenus() {
foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
$ptype_obj = get_post_type_object( $ptype );
if ( ! $ptype_obj->show_in_menu || true === $ptype_obj->show_in_menu ) {
add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
* Registers support of certain features for a post type.
* All core features are directly associated with a functional area of the edit
* screen, such as the editor or a meta box. Features include: 'title', 'editor',
* 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',
* 'thumbnail', 'custom-fields', and 'post-formats'.
* Additionally, the 'revisions' feature dictates whether the post type will
* store revisions, and the 'comments' feature dictates whether the comments
* count will show on the edit screen.
* A third, optional parameter can also be passed along with a feature to provide
* additional information about supporting that feature.
* add_post_type_support( 'my_post_type', 'comments' );
* add_post_type_support( 'my_post_type', array(
* add_post_type_support( 'my_post_type', 'my_feature', array(
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter
* by adding it to the function signature.
* @global array $_wp_post_type_features
* @param string $post_type The post type for which to add the feature.
* @param string|array $feature The feature being added, accepts an array of
* feature strings or a single string.
* @param mixed ...$args Optional extra arguments to pass along with certain features.
function add_post_type_support( $post_type, $feature, ...$args ) {
global $_wp_post_type_features;
$features = (array) $feature;
foreach ( $features as $feature ) {
$_wp_post_type_features[ $post_type ][ $feature ] = $args;
$_wp_post_type_features[ $post_type ][ $feature ] = true;
* Remove support for a feature from a post type.
* @global array $_wp_post_type_features
* @param string $post_type The post type for which to remove the feature.
* @param string $feature The feature being removed.
function remove_post_type_support( $post_type, $feature ) {
global $_wp_post_type_features;
unset( $_wp_post_type_features[ $post_type ][ $feature ] );
* Get all the post type features
* @global array $_wp_post_type_features
* @param string $post_type The post type.
* @return array Post type supports list.
function get_all_post_type_supports( $post_type ) {
global $_wp_post_type_features;
if ( isset( $_wp_post_type_features[ $post_type ] ) ) {
return $_wp_post_type_features[ $post_type ];
* Check a post type's support for a given feature.
* @global array $_wp_post_type_features
* @param string $post_type The post type being checked.
* @param string $feature The feature being checked.
* @return bool Whether the post type supports the given feature.
function post_type_supports( $post_type, $feature ) {
global $_wp_post_type_features;
return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) );
* Retrieves a list of post type names that support a specific feature.
* @global array $_wp_post_type_features Post type features
* @param array|string $feature Single feature or an array of features the post types should support.
* @param string $operator Optional. The logical operation to perform. 'or' means
* only one element from the array needs to match; 'and'
* means all elements must match; 'not' means no elements may
* @return string[] A list of post type names.
function get_post_types_by_support( $feature, $operator = 'and' ) {
global $_wp_post_type_features;
$features = array_fill_keys( (array) $feature, true );
return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
* Update the post type for the post ID.
* The page or post cache will be cleaned for the post ID.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $post_id Optional. Post ID to change post type. Default 0.
* @param string $post_type Optional. Post type. Accepts 'post' or 'page' to
* name a few. Default 'post'.
* @return int|false Amount of rows changed. Should be 1 for success and 0 for failure.
function set_post_type( $post_id = 0, $post_type = 'post' ) {
$post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' );
$return = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) );
clean_post_cache( $post_id );