Post type refers to the various structured data that is maintained in the WordPress posts table. To create a new or modify an existing post type, 'register_post_type'
function is used.
WordPress already has some built-in post types are post
, page
, attachment
, revision
, and nav-menu-item
.
Function Reference:
For a quick reference of how to use 'register_post_type'
, dig your own WordPress directory wp-includes folder. Find the PHP file post.php and find line 17 in it (in wordpress v 3.1). Here you can check how WordPress has used 'register_post_type'
for creating the initial post types such as post
, page
, attachment
etc. You can use them as reference but remember to set the '_builtin'
argument to “false”.
Using register_post_type
:
To register a new post type, you will need a name and an array of parameters. The function will need to be used within a function hooked to ‘init’ action.
add_action('init', 'w4dev_post__register_init'); function w4dev_post__register_init() { $post_type_name = ''; $arguments = array(); register_post_type( $post_type_name, $arguments ); }
Use small letter for $post_type_name
naming post type while registering. You can find the argument parameter in WordPress Codex Page
Arguments of register_post_type
function:
show_ui
Value ‘true’ for 'show_ui'
will generate a admin panel option panel to manage this post type. If you want to manage custom post type with your own layout/option/process/functionality, then set this ‘false’ and use ‘_edit_link’ ( example of edit link: '_edit_link' => 'post.php?post=%d'
. where %d will be replaced with the post id. ) argument to direct the edit link for this post type to your own url.
supports
Register support of certain features for a post type. All features are directly associated with a functional area of the edit screen, such as the editor or a meta box: 'title'
, editor
, comments
, revisions
, trackbacks
, author
, excerpt
, page-attributes
, thumbnail
, and custom-fields
. 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.
rewrite
Rewrite is the url formation you want to use for this post type. Example: A post url may looks like – http://SITEURL/post_type/post_name/
, generally it doesn’t happens as most of the time only the post name is used. ‘rewrite’
accept parameter in array. Ex: ‘rewrite’ => array(‘slug’ => ‘custom_post’)
will generate url for this post similar to 'http://SITEURL/custom_post/post_name/'
.
capability_type
The string to use to build the read, edit, and delete capabilities for this post type, most likely your post type objects name.
capabilities
Add additional capability or write all of them manually. There is 7 capabilities for a post object type by default. These 7 capabilities are generated by combining singular or plural form of given capability_type
string.
Example: A capability_type ‘article’ will generate capabilities – edit_article
, read_article
, delete_article
, edit_articles
, edit_others_articles
, publish_articles
, read_private_articles
.
map_meta_cap
As like the 7 capabilities, map_meta_cap
generates 7 more capabilities post this. Setting map_meta_cap
to ‘false’ will skip adding meta capabilities automatically. Meta capabilities are based on Plural form of capability_type String.
The 7 default meta capabilities –
read
, delete_articles
, delete_private_articles
, delete_published_articles
, delete_others_articles
, edit_private_articles
, edit_published_articles
.