Custom Post Type Service Provider
This document describes the properties of a Custom Post Type Service Provider in WordPress. It includes details and examples for properties such as autoSaveRestControllerClass
, canExport
, and capabilities
, explaining their purposes and default values.
For more details about how to create a Custom Post Type Service Provider, please visit the Service Provider page.
Properties
autoSaveRestControllerClass
/**
* To change the controller class of REST API route for autosaves.
* Default is 'WP_REST_Autosaves_Controller'.
*
* @var string
*/
protected $autoSaveRestControllerClass;
This property allows you to specify a custom controller class for handling autosave requests via the REST API.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $autoSaveRestControllerClass = 'My_Custom_Autosaves_Controller';
}
canExport
/**
* Allows this post type to be exported. Defaults to true.
*
* @var bool
*/
protected $canExport;
This property determines whether the custom post type can be exported using the WordPress export tool.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $canExport = true;
}
capabilities
/**
* Array of capabilities for this post type.
* By default, the capability_type is used as a base to construct capabilities.
* You can see accepted values in {@link get_post_type_capabilities()}.
*
* @var array
*/
protected $capabilities = [];
This property allows you to define custom capabilities for the post type, which can be used to control access and permissions.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $capabilities = [
'edit_posts' => 'edit_my_custom_posts',
'publish_posts' => 'publish_my_custom_posts',
];
}
capabilityType
/**
* The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
* May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
* capabilities, e.g. array('story', 'stories').
*
* @var string
*/
protected $capabilityType;
This property sets the base capability type for the post type, which is used to generate specific capabilities.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $capabilityType = 'my_custom_post';
}
deleteWithUser
/**
* Whether to delete posts of this type when deleting a user.
*
* If true, posts of this type belonging to the user will be moved to trash when the user is deleted.
* If false, posts of this type belonging to the user will *not* be trashed or deleted.
* If not set (the default), posts are trashed if post_type_supports('author').
* Otherwise posts are not trashed or deleted.
*
* @var bool
*/
protected $deleteWithUser;
This property determines whether posts of this type should be deleted when the associated user is deleted.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $deleteWithUser = true;
}
description
/**
* A short descriptive summary of what the post type is. Defaults to blank.
*
* @var string
*/
protected $description = '';
This property provides a brief description of the custom post type, which can be displayed in the WordPress admin.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $description = 'This is a custom post type for managing custom content.';
}
epMask
/**
* Assign an endpoint mask.
* If not specified and permalink_epmask is set, inherits from permalink_epmask.
* If not specified and permalink_epmask is not set, defaults to EP_PERMALINK
*
* @var int
*/
protected $epMask = EP_PERMALINK;
This property sets the endpoint mask for the custom post type, which determines how WordPress handles URL endpoints.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $epMask = EP_ALL;
}
excludeFromSearch
/**
* Whether to exclude posts with this post type from front end search results.
* If not set, the opposite of public's current value is used.
*
* @var bool
*/
protected $excludeFromSearch;
This property determines whether posts of this type should be excluded from search results on the front end.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $excludeFromSearch = true;
}
feeds
/**
* Should a feed permastruct be built for this post type. Inherits default from has_archive.
*
* @var bool
*/
protected $feeds;
This property specifies whether a feed should be generated for this post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $feeds = true;
}
hasArchive
/**
* True to enable post type archives. Default is false.
* Will generate the proper rewrite rules if rewrite is enabled.
*
* @var bool
*/
protected $hasArchive;
This property enables archive pages for the custom post type, allowing users to view a list of posts of this type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $hasArchive = true;
}
hierarchical
/**
* Whether the post type is hierarchical (e.g. page).
* Defaults to false.
*
* @var bool
*/
protected $hierarchical;
This property indicates whether the post type is hierarchical, allowing for parent-child relationships between posts.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $hierarchical = true;
}
id
/**
* Post type key. Must not exceed 20 characters and may only contain
* lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().
*
* `register_post_type( $post_type, $args = array() )`
*
* @var string
*/
protected $id = '';
This property holds the unique identifier for the custom post type, used when registering the post type with WordPress.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $id = 'my_custom_post_type';
}
labels
/**
* An array of labels for this post type.
* If not set, post labels are inherited for non-hierarchical types and page labels for hierarchical ones.
* You can see accepted values in {@link get_post_type_labels()}.
*
* By using the property $labels you can set just the labels you want to change.
* The final labels will be merged with the default labels.
* If you want to handle all labels, you can override the method `registerLabels()`.
*
* @example
*
* class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
* protected $labels = [
* 'name' => 'My Custom Post Types',
* 'singular_name' => 'My Custom Post Type',
* ];
* }
*
* or
*
* class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
* public function boot() {
* $this->labels = [
* 'name' => 'My Custom Post Types',
* 'singular_name' => 'My Custom Post Type',
* ];
* }
*
*
*
* @var array
*/
protected $labels = [];
This property contains an array of labels used throughout the WordPress admin interface for the custom post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $labels = [
'name' => 'Custom Posts',
'singular_name' => 'Custom Post',
];
}
lateRouteRegistration
/**
* A flag to direct the REST API controllers for autosave / revisions
* should be registered before/after the post type controller.
*
* @var string
*/
protected $lateRouteRegistration;
This property controls the order of REST API route registration for autosave and revision controllers relative to the main post type controller.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $lateRouteRegistration = 'after';
}
mapMetaCap
/**
* Whether to use the internal default meta capability handling. Defaults to false.
*
* @var bool
*/
protected $mapMetaCap;
This property determines whether WordPress should use its default meta capability handling for the post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $mapMetaCap = true;
}
menuIcon
/**
* The url to the icon to be used for this menu. Defaults to use the posts icon.
* Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
* This should begin with 'data:image/svg+xml;base64,'.
* Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-piechart'.
* Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
*
* @var string
*/
protected $menuIcon = '';
This property sets the icon for the custom post type in the WordPress admin menu.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $menuIcon = 'dashicons-admin-post';
}
menuPosition
/**
* The position in the menu order the post type should appear.
* show_in_menu must be true
* Defaults to null, which places it at the bottom of its area.
*
* @var null
*/
protected $menuPosition;
This property specifies the position of the custom post type in the WordPress admin menu.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $menuPosition = 5;
}
name
/**
* Name of the post type shown in the menu. Usually plural.
*
* @var string
*/
protected $name = '';
This property holds the name of the custom post type as displayed in the WordPress admin menu.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $name = 'Custom Posts';
}
pages
/**
* Should the permastruct provide for pagination. Defaults to true.
*
* @var bool
*/
protected $pages;
This property indicates whether pagination should be enabled for the custom post type’s permalink structure.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $pages = true;
}
plural
/**
* Name of the post type shown in the menu as plural.
*
* @var string
*/
protected $plural = '';
This property holds the plural form of the custom post type name, used in the WordPress admin menu.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $plural = 'Custom Posts';
}
public
/**
* Whether a post type is intended for use publicly either via the admin interface or by front-end users.
* Defaults to false.
* While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are
* inherited from public, each does not rely on this relationship and controls a very specific intention.
*
* @var bool
*/
protected $public = false;
This property determines whether the custom post type is publicly accessible through the WordPress admin interface and front-end.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $public = true;
}
publiclyQueryable
/**
* Whether queries can be performed on the front end for the post type as part of parse_request().
*
* ?post_type={post_type_key}
* ?{post_type_key}={single_post_slug}
* ?{post_type_query_var}={single_post_slug}
*
* If not set, the default is inherited from public.
*
* @var bool
*/
protected $publiclyQueryable;
This property specifies whether the custom post type can be queried on the front end.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $publiclyQueryable = true;
}
queryVar
/**
* Sets the query_var key for this post type. Defaults to $post_type key
* If false, a post type cannot be loaded at ?{query_var}={post_slug}
* If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
*
* @var string
*/
protected $queryVar;
This property sets the query variable for the custom post type, allowing it to be accessed via query strings.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $queryVar = 'my_custom_post';
}
restBase
/**
* To change the base url of REST API route.
* Default is the post type key.
*
* @var string
*/
protected $restBase;
This property sets the base URL for the REST API routes associated with the custom post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $restBase = 'custom_posts';
}
restControllerClass
/**
* To change the controller class of REST API route.
* Default is 'WP_REST_Posts_Controller'.
*
* @var string
*/
protected $restControllerClass;
This property specifies a custom controller class for handling REST API requests for the custom post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $restControllerClass = 'My_Custom_REST_Controller';
}
restNamespace
/**
* To change the namespace of REST API route.
* Default is 'wp/v2'.
*
* @var string
*/
protected $restNamespace;
This property sets the namespace for the REST API routes associated with the custom post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $restNamespace = 'my_namespace/v1';
}
revisionsRestControllerClass
/**
* To change the controller class of REST API route for revisions.
* Default is 'WP_REST_Revisions_Controller'.
*
* @var string
*/
protected $revisionsRestControllerClass;
This property allows you to specify a custom controller class for handling revision requests via the REST API.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $revisionsRestControllerClass = 'My_Custom_Revisions_Controller';
}
rewrite
/**
* Triggers the handling of rewrites for this post type. Defaults to true, using $post_type as slug.
* To prevent rewrite, set to false.
* To specify rewrite rules, an array can be passed with any of these keys
* 'slug' => string Customize the permastruct slug. Defaults to $post_type key
* 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
* 'feeds' => bool Should a feed permastruct be built for this post type. Inherits default from has_archive.
* 'pages' => bool Should the permastruct provide for pagination. Defaults to true.
* 'ep_mask' => const Assign an endpoint mask.
* If not specified and permalink_epmask is set, inherits from permalink_epmask.
* If not specified and permalink_epmask is not set, defaults to EP_PERMALINK
*
* @var array
*/
protected $rewrite = [];
This property defines the rewrite rules for the custom post type, controlling how URLs are generated and handled.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $rewrite = [
'slug' => 'custom-post',
'with_front' => false,
];
}
showInAdminBar
/**
* Makes this post type available via the admin bar.
* If not set, the default is inherited from showInMenu
*
* @var bool
*/
protected $showInAdminBar;
This property determines whether the custom post type should be accessible from the WordPress admin bar.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $showInAdminBar = true;
}
showInMenu
/**
* Where to show the post type in the admin menu.
* If true, the post type is shown in its own top level menu.
* If false, no menu is shown
* If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type will be
* placed as a sub menu of that. show_ui must be true. If not set, the default is inherited from show_ui
*
* @var bool
*/
protected $showInMenu;
This property specifies where the custom post type should appear in the WordPress admin menu.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $showInMenu = true;
}
showInNavMenus
/**
* Makes this post type available for selection in navigation menus.
* If not set, the default is inherited from public.
*
* @var bool
*/
protected $showInNavMenus;
This property indicates whether the custom post type should be available for selection in WordPress navigation menus.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $showInNavMenus = true;
}
showInRest
/**
* Whether to include the post type in the REST API.
* Set this to true for the post type to be available in the block editor.
*
* @var bool
*/
protected $showInRest;
This property determines whether the custom post type should be available via the WordPress REST API.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $showInRest = true;
}
showUI
/**
* Whether to generate a default UI for managing this post type in the admin.
* If not set, the default is inherited from public.
*
* @var bool
*/
protected $showUI;
This property specifies whether a default user interface should be generated for managing the custom post type in the WordPress admin.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $showUI = true;
}
slug
/**
* Customize the permastruct slug. Defaults to $post_type key
*
* @var string
*/
protected $slug;
This property sets a custom slug for the custom post type’s permalink structure.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $slug = 'custom-post';
}
supports
/**
* An alias for calling add_post_type_support() directly. Defaults to title and editor.
* See {@link add_post_type_support()} for documentation.
*
* @var array
*/
protected $supports = [];
This property defines the features that the custom post type supports, such as title, editor, thumbnail, etc.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $supports = ['title', 'editor', 'thumbnail'];
}
taxonomies
/**
* An array of taxonomy identifiers that will be registered for the post type.
* Default is no taxonomies.
* Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
*
* @var array
*/
protected $taxonomies = [];
This property lists the taxonomies that should be associated with the custom post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $taxonomies = ['category', 'post_tag'];
}
template
/**
* Array of blocks to use as the default initial state for an editor
* session. Each item should be an array containing block name and
* optional attributes. Default empty array.
*
* @var array
*/
protected $template;
This property defines the default block template for the custom post type, specifying which blocks should be included by default.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $template = [
['core/paragraph', ['placeholder' => 'Add a description...']],
];
}
templateLock
/**
* Whether the block template should be locked if $template is set.
* - If set to 'all', the user is unable to insert new blocks,
* move existing blocks and delete blocks.
* - If set to 'insert', the user is able to move existing blocks
* but is unable to insert new blocks and delete blocks.
* Default false.
*
* @var string|bool
*/
protected $templateLock;
This property controls whether the block template is locked, restricting the user’s ability to modify the block layout.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $templateLock = 'all';
}
withFront
/**
* Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
*
* @var bool
*/
protected $withFront;
This property determines whether the permalink structure should be prepended with the front base.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
protected $withFront = false;
}
Methods
boot
/**
* You may override this method in order to register your own actions and filters.
*/
public function boot()
This method can be overridden to register custom actions and filters for the custom post type. It’s typically used to hook into WordPress actions and filters to extend or modify functionality.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function boot() {
$this->plural = 'Spaceships';
}
}
columnContent
/**
* Return the column content
*
* @param string $column_id
* @param string $value
* @param object $post
*
* @since 1.9.0
* @return string
*/
public function columnContent($column_id, $value, $post)
This method returns the content for a specific column in the post type table. It can be overridden to customize the output of custom columns in the admin post list table.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function columnContent($column_id, $value, $post) {
if ($column_id === 'my_custom_column') {
return "<h1>$value</h1>";
}
return $value;
}
}
is
/**
* Return TRUE if this custom post type is current view.
*
* @return bool
*/
public function is(): bool
This method checks if the current view is for this custom post type. It is useful for conditionally executing code based on the current post type context.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function someFunction() {
if ($this->is()) {
// Do something specific for this post type
}
}
}
registerAfterTitleView
/**
* Add content after title
*
* @since 1.9.0
* @return void
*/
public function registerAfterTitleView()
This method can be overridden to add custom content after the title in the post edit screen. It’s useful for adding additional instructions or elements to the post editor interface.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function registerAfterTitleView() {
echo '<p>Custom instructions or content here.</p>';
}
}
registerColumns
/**
* You may override this method in order to register your own columns.
*
* @param array $columns
*
* @since 1.9.0
*
* @return array
*/
public function registerColumns($columns)
This method can be overridden to register custom columns for the post type table. It allows you to add or modify columns in the admin post list table.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function registerColumns($columns) {
$columns['my_custom_column'] = 'Custom Column';
return $columns;
}
}
registerLabels
/**
* You may override this method in order to register your own labels.
*
* @param array $defaults Default labels
*
* @example
*
* class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
* public function registerLabels($defaults) {
*
* // You will use just the labels you want to change
* // Otherwise you have to merge manually with the default labels
* return [
* 'name' => 'My Custom Post Types',
* 'singular_name' => 'My Custom Post Type',
* 'menu_name' => 'Custom Posts',
* ];
* }
* }
*
*
* @since 1.9.0
* @return array
*/
public function registerLabels($defaults)
This method can be overridden to provide custom labels for the post type. These labels are used throughout the WordPress admin interface.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function registerLabels($defaults) {
// You may also merge the defaults with your own labels
return [
'name' => 'My Custom Post Types',
'singular_name' => 'My Custom Post Type',
'menu_name' => 'Custom Posts',
];
}
}
registerMetaBoxes
/**
* Register meta boxes.
*
* $meta_box = [
* 'id' => 'my_meta_box',
* 'title' => 'My Meta Box',
* 'view' => 'my_meta_box_view',
* 'context' => 'normal',
* 'priority' => 'high',
* 'callback_args' => null,
* ];
*
* @since 1.9.0
* @return array
*/
public function registerMetaBoxes()
This method can be overridden to register custom meta boxes for the post type. Meta boxes allow you to add custom fields and controls to the post editing screen.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function registerMetaBoxes() {
return [
[
'id' => 'my_meta_box',
'title' => 'My Meta Box',
'view' => [$this, 'displayMetaBox'],
'context' => 'normal',
'priority' => 'high',
]
];
}
public function displayMetaBox($post) {
echo '<input type="text" name="my_meta_field" value="' . get_post_meta($post->ID, '_my_meta_field', true) . '">';
}
}
registerPlaceholderTitle
/**
* Return the placeholder title.
* You may override this method to return your own placeholder title.
*
* @param string $placeholder Default 'Enter title here'.
*
* @since 1.9.0
*
* @return string
*/
public function registerPlaceholderTitle($placeholder)
This method can be overridden to customize the placeholder text for the title field in the post editor.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function registerPlaceholderTitle($placeholder) {
return 'Enter your custom post title here';
}
}
registerPostMeta
/**
* Register post meta
*
* @since 1.9.0
*/
public function registerPostMeta()
This method can be overridden to register custom post meta fields. It allows you to define metadata that can be associated with your custom post type.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function registerPostMeta() {
return [
'_my_meta_key' => [
'type' => 'string',
'description' => 'A custom meta field',
'single' => true,
'show_in_rest' => true,
]
];
}
}
update
/**
* Override this method to save/update your custom data.
* This method is called by hook action save_post_{post_type}`
*
* @param int|string $post_id Post ID
* @param object $post Optional. Post object
*/
public function update($post_id, $post)
This method can be overridden to handle custom data saving when a post of this type is saved or updated. It provides a safe place to implement custom save logic.
Example:
class MyCustomPostType extends WordPressCustomPostTypeServiceProvider {
public function update($post_id, $post) {
if (isset($_POST['my_meta_field'])) {
update_post_meta($post_id, '_my_meta_field', sanitize_text_field($_POST['my_meta_field']));
}
}
}