Post Types API Functions

Core functions for post type registration and management.

Source: wp-includes/post.php


register_post_type()

Registers a new post type. Must be called during the init action.

php
register_post_type( string $post_type, array|string $args = array() ): WP_Post_Type|WP_Error

Since: 2.9.0
Since: 4.6.0 Returns WP_Post_Type object instead of object/stdClass.

Parameters

ParameterTypeDescription
$post_typestringPost type key. Max 20 characters, lowercase alphanumeric, dashes, underscores.
$argsarray|stringConfiguration array

Args

KeyTypeDefaultDescription
labelstringValue of $labels['name']Name shown in menu (usually plural)
labelsarray[]Array of labels. See get_post_type_labels()
descriptionstring''Short descriptive summary
publicboolfalseWhether intended for public use
hierarchicalboolfalseWhether hierarchical (like pages)
exclude_from_searchbool!$publicExclude from front-end search
publicly_queryablebool$publicAllow front-end queries
show_uibool$publicGenerate admin UI
show_in_menubool|string$show_uiWhere to show in admin menu
show_in_nav_menusbool$publicAvailable in navigation menus
show_in_admin_barbool$show_in_menuAvailable in admin bar
show_in_restboolfalseInclude in REST API (required for block editor)
rest_basestring$post_typeREST API base URL
rest_namespacestring'wp/v2'REST API namespace
rest_controller_classstring'WP_REST_Posts_Controller'REST controller class
menu_positionintnullPosition in admin menu
menu_iconstringnullDashicon name or base64 SVG data URI
capability_typestring|array'post'Base for capability generation
capabilitiesarray[]Custom capabilities
map_meta_capboolfalseUse internal meta capability handling
supportsarray|false['title', 'editor']Features to support
register_meta_box_cbcallablenullCallback for meta box setup
taxonomiesarray[]Taxonomies to register
has_archivebool|stringfalseEnable archive pages
rewritebool|arraytrueRewrite rules configuration
query_varstring|bool$post_typeQuery var key
can_exportbooltrueAllow export
delete_with_userboolnullDelete posts when user deleted
templatearray[]Default block template
template_lockstring|falsefalseTemplate lock: 'all', 'insert', or false
embeddablebool$publicWhether embeddable (since 6.8.0)

Supports Array

FeatureDescription
titlePost title
editorContent editor (implies autosave)
authorAuthor selection
thumbnailFeatured image
excerptExcerpt field
trackbacksTrackbacks/pingbacks
custom-fieldsCustom fields meta box
commentsComments
revisionsStore revisions
page-attributesMenu order, parent (hierarchical)
post-formatsPost format support
autosaveAutosave support

Rewrite Array

KeyTypeDefaultDescription
slugstring$post_typePermalink slug
with_frontbooltruePrepend front base
feedsbool$has_archiveAdd feed rewrites
pagesbooltrueAdd pagination rewrites
ep_maskintEP_PERMALINKEndpoint mask

Returns

WP_Post_Type on success, WP_Error on failure.

Example

php
add_action( 'init', function() {
    register_post_type( 'book', array(
        'labels'             => array(
            'name'          => __( 'Books' ),
            'singular_name' => __( 'Book' ),
            'add_new_item'  => __( 'Add New Book' ),
            'edit_item'     => __( 'Edit Book' ),
        ),
        'public'             => true,
        'show_in_rest'       => true,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'taxonomies'         => array( 'category', 'post_tag' ),
        'has_archive'        => true,
        'rewrite'            => array( 'slug' => 'books' ),
        'menu_icon'          => 'dashicons-book',
        'menu_position'      => 5,
    ) );
} );

unregister_post_type()

Unregisters a post type. Cannot unregister built-in types.

php
unregister_post_type( string $post_type ): true|WP_Error

Since: 4.5.0

Parameters

ParameterTypeDescription
$post_typestringPost type to unregister

Returns

true on success, WP_Error on failure.


get_post_type()

Retrieves the post type of a post.

php
get_post_type( int|WP_Post|null $post = null ): string|false

Since: 2.1.0

Parameters

ParameterTypeDescription
$postint|WP_Post|nullPost ID or object. Default current post.

Returns

Post type string on success, false on failure.


get_post_type_object()

Retrieves a post type object by name.

php
get_post_type_object( string $post_type ): WP_Post_Type|null

Since: 3.0.0
Since: 4.6.0 Returns WP_Post_Type instance.

Parameters

ParameterTypeDescription
$post_typestringRegistered post type name

Returns

WP_Post_Type object or null if not found.


get_post_types()

Gets all registered post type objects.

php
get_post_types( array|string $args = array(), string $output = 'names', string $operator = 'and' ): string[]|WP_Post_Type[]

Since: 2.9.0

Parameters

ParameterTypeDefaultDescription
$argsarray|string[]Arguments to filter by
$outputstring'names''names' or 'objects'
$operatorstring'and''and', 'or', or 'not'

Returns

Array of post type names or WP_Post_Type objects.

Example

php
// Get all public post types
$public_types = get_post_types( array( 'public' => true ), 'objects' );

// Get all post types with REST API support
$rest_types = get_post_types( array( 'show_in_rest' => true ) );

post_type_exists()

Checks if a post type is registered.

php
post_type_exists( string $post_type ): bool

Since: 3.0.0

Parameters

ParameterTypeDescription
$post_typestringPost type name

Returns

true if registered, false otherwise.


is_post_type_hierarchical()

Checks if a post type is hierarchical.

php
is_post_type_hierarchical( string $post_type ): bool

Since: 3.0.0

Parameters

ParameterTypeDescription
$post_typestringPost type name

Returns

true if hierarchical, false otherwise.


is_post_type_viewable()

Determines if a post type is considered "viewable".

php
is_post_type_viewable( string|WP_Post_Type $post_type ): bool

Since: 4.4.0
Since: 5.9.0 Added is_post_type_viewable filter.

Parameters

ParameterTypeDescription
$post_typestring|WP_Post_TypePost type name or object

Returns

true if viewable, false otherwise.


get_post_type_capabilities()

Builds capabilities object from post type args.

php
get_post_type_capabilities( object $args ): object

Since: 3.0.0

Parameters

ParameterTypeDescription
$argsobjectPost type registration args

Returns

Object with capability properties:

CapabilityDescription
edit_postMeta capability for editing
read_postMeta capability for reading
delete_postMeta capability for deleting
edit_postsEdit posts of this type
edit_others_postsEdit others’ posts
delete_postsDelete posts
publish_postsPublish posts
read_private_postsRead private posts
create_postsCreate new posts

get_post_type_labels()

Builds labels object for a post type.

php
get_post_type_labels( object|WP_Post_Type $post_type_object ): object

Since: 3.0.0

Label Keys

KeyDescription
nameGeneral name (plural)
singular_nameSingular name
add_newAdd new label
add_new_itemAdd new item
edit_itemEdit item
new_itemNew item
view_itemView item
view_itemsView items
search_itemsSearch items
not_foundNot found
not_found_in_trashNot found in trash
parent_item_colonParent item (hierarchical)
all_itemsAll items
archivesArchives
attributesAttributes
insert_into_itemInsert into item
uploaded_to_this_itemUploaded to this item
featured_imageFeatured image
set_featured_imageSet featured image
remove_featured_imageRemove featured image
use_featured_imageUse as featured image
menu_nameMenu name
filter_items_listFilter items list
filter_by_dateFilter by date
items_list_navigationItems list navigation
items_listItems list
item_publishedItem published
item_published_privatelyItem published privately
item_reverted_to_draftItem reverted to draft
item_trashedItem trashed (since 6.3.0)
item_scheduledItem scheduled
item_updatedItem updated
item_linkItem link
item_link_descriptionItem link description
template_nameTemplate name (since 6.6.0)

add_post_type_support()

Registers feature support for a post type.

php
add_post_type_support( string $post_type, string|array $feature, mixed ...$args ): void

Since: 3.0.0

Parameters

ParameterTypeDescription
$post_typestringPost type
$featurestring|arrayFeature(s) to add
...$argsmixedAdditional feature arguments

Example

php
add_post_type_support( 'page', 'excerpt' );
add_post_type_support( 'my_cpt', array( 'thumbnail', 'comments' ) );
add_post_type_support( 'my_cpt', 'custom_feature', array( 'option' => 'value' ) );

remove_post_type_support()

Removes feature support from a post type.

php
remove_post_type_support( string $post_type, string $feature ): void

Since: 3.0.0

Parameters

ParameterTypeDescription
$post_typestringPost type
$featurestringFeature to remove

Example

php
// Remove editor from pages
add_action( 'init', function() {
    remove_post_type_support( 'page', 'editor' );
} );

post_type_supports()

Checks if a post type supports a feature.

php
post_type_supports( string $post_type, string $feature ): bool

Since: 3.0.0

Parameters

ParameterTypeDescription
$post_typestringPost type
$featurestringFeature to check

Returns

true if supported, false otherwise.


get_all_post_type_supports()

Gets all features a post type supports.

php
get_all_post_type_supports( string $post_type ): array

Since: 3.4.0

Parameters

ParameterTypeDescription
$post_typestringPost type

Returns

Array of supported features.


get_post_types_by_support()

Gets post types that support specific feature(s).

php
get_post_types_by_support( array|string $feature, string $operator = 'and' ): string[]

Since: 4.5.0

Parameters

ParameterTypeDefaultDescription
$featurearray|stringFeature(s) to check
$operatorstring'and''and', 'or', or 'not'

Returns

Array of post type names.

Example

php
// Get all post types with revisions
$revisioned = get_post_types_by_support( 'revisions' );

// Get post types with both title and editor
$editable = get_post_types_by_support( array( 'title', 'editor' ) );

set_post_type()

Updates the post type for a post.

php
set_post_type( int $post_id = 0, string $post_type = 'post' ): int|false

Since: 2.5.0

Parameters

ParameterTypeDefaultDescription
$post_idint0Post ID
$post_typestring'post'New post type

Returns

Number of rows changed (1 for success, 0 for failure), or false.


is_post_publicly_viewable()

Checks if a specific post is publicly viewable.

php
is_post_publicly_viewable( int|WP_Post|null $post = null ): bool

Since: 5.7.0

Parameters

ParameterTypeDescription
$postint|WP_Post|nullPost ID or object

Returns

true if publicly viewable, false otherwise.


is_post_embeddable()

Checks if a post is embeddable.

php
is_post_embeddable( int|WP_Post|null $post = null ): bool

Since: 6.8.0

Parameters

ParameterTypeDescription
$postint|WP_Post|nullPost ID or object

Returns

true if embeddable, false otherwise.