WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/register post type

Description

Create or modify a post type. Do not use register_post_type before init.

Usage

<?php register_post_type$post_type$args ?>

Parameters

$post_type
(string) (required) Post type. (max. 20 characters)
Default: None
$args
(array) (optional) An array of arguments.
Default: None

Arguments

label
(string) (optional) A plural descriptive name for the post type marked for translation.
Default: $post_type
labels
(array) (optional) labels - An array of labels for this post type. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
Default: if empty, name is set to label value, and singular_name is set to name value
  • 'name' - general name for the post type, usually plural. The same as, and overridden by $post_type_object->label
  • 'singular_name' - name for one object of this post type. Defaults to value of name
  • 'add_new' - the add new text. The default is Add New for both hierarchical and non-hierarchical types. When internationalizing this string, please use a gettext context matching your post type. Example: _x('Add New', 'product');
  • 'add_new_item' - the add new item text. Default is Add New Post/Add New Page
  • 'edit_item' - the edit item text. Default is Edit Post/Edit Page
  • 'new_item' - the new item text. Default is New Post/New Page
  • 'view_item' - the view item text. Default is View Post/View Page
  • 'search_items' - the search items text. Default is Search Posts/Search Pages
  • 'not_found' - the not found text. Default is No posts found/No pages found
  • 'not_found_in_trash' - the not found in trash text. Default is No posts found in Trash/No pages found in Trash
  • 'parent_item_colon' - the parent text. This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
description
(string) (optional) A short descriptive summary of what the post type is.
Default: blank
public
(boolean) (optional) Meta argument used to define default values for publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search.
Default: false
  • 'false' - do not display a user-interface for this post type (show_ui=false), post_type queries can not be performed from the front end (publicly_queryable=false), exclude posts with this post type from search results (exclude_from_search=true), hide post_type for selection in navigation menus (show_in_nav_menus=false)
  • 'true' - show_ui=true, publicly_queryable=true, exclude_from_search=false, show_in_nav_menus=true
publicly_queryable
(boolean) (optional) Whether post_type queries can be performed from the front end.
Default: value of public argument
exclude_from_search
(boolean) (importance) Whether to exclude posts with this post type from search results.
Default: value of the opposite of the public argument
show_ui
(boolean) (optional) Whether to generate a default UI for managing this post type. Note that _built-in post types, such as post and page, are intentionally set to false.
Default: value of public argument
  • 'false' - do not display a user-interface for this post type
  • 'true' - display a user-interface (admin panel) for this post type
capability_type
(string) (optional) The post type to use for checking read, edit, and delete capabilities.
Default: "post"

By default the capability_type is used to construct capabilities.

capabilities
(array) (optional) An array of the capabilities for this post type.
Default: capability_type is used to construct
  • 'edit_' . $capability_type (edit_post) - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type"
  • 'edit_' . $capability_type . 's' (edit_posts) - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s"
  • 'edit_others_' . $capability_type . 's' (edit_others_posts) - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s"
  • 'publish_' . $capability_type . 's' (publish_posts) - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s"
  • 'read_' . $capability_type (read_post) - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type"
  • 'read_private_' . $capability_type . 's' (read_private_posts) - The capability that controls reading private posts. Defaults to "read_private . $capability_type . s"
  • 'delete_' . $capability_type (delete_post) - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type"
hierarchical
(boolean) (optional) Whether the post type is hierarchical. Allows Parent to be specified.
Default: false
supports
(array) (optional) An alias for calling add_post_type_support() directly.
Default: title and editor
  • 'title'
  • 'editor' (content)
  • 'author'
  • 'thumbnail' (featured image) (current theme must also support post-thumbnails)
  • 'excerpt'
  • 'trackbacks'
  • 'custom-fields'
  • 'comments' (also will see comment count balloon on edit screen)
  • 'revisions' (will store revisions)
  • 'page-attributes' (template and menu order) (hierarchical must be true)
register_meta_box_cb
(string) (optional) Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
Default: None
taxonomies
(array) (optional) An array of registered taxonomies like category or post_tag that will be used with this post type. This can be use in lieu of calling register_taxonomy_for_object_type() directly. Taxonomies still need to be registered with register_taxonomy().
Default: None
menu_position
(integer) (optional) The position in the menu order the post type should appear.
Default: null - defaults to below Comments
  • 5 - below Posts
  • 10 - below Media
  • 20 - below Pages
  • 60 - below first separator
  • 100 - below second separator
menu_icon
(string) (optional) The url to the icon to be used for this menu.
Default: null - defaults to the posts icon
permalink_epmask
(string) (optional) The default rewrite endpoint bitmasks. For more info see Trac Ticket 12605.
Default: EP_PERMALINK
rewrite
(boolean or array) (optional) Rewrite permalinks with this format.
Default: true
$args array
  • 'slug' - prepend posts with this slug - defaults to post type's name
  • 'with_front' - allowing permalinks to be prepended with front base (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/) - defaults to true
query_var
(boolean) (optional) Name of the query var to use for this post type.
Default: true
can_export
(boolean) (optional) Can this post_type be exported.
Default: true
show_in_nav_menus
(boolean) (optional) Whether post_type is available for selection in navigation menus.
Default: value of public argument
_builtin
(boolean) (not for general use) Whether this post type is a native or "built-in" post_type. Note: this Codex entry is for documentation - core developers recommend you don't use this when registering your own post type
Default: false
  • 'false' - default this is a custom post type
  • 'true' - this is a built-in native post type (post, page, attachment, revision, nav_menu_item)
_edit_link
(boolean) (not for general use) Link to edit an entry with this post type. Note: this Codex entry is for documentation '-' core developers recommend you don't use this when registering your own post type
Default:
  • 'post.php?post=%d'

Example

An an example of registering a post type called "book" including providing contextual help:

<?php
add_action('init', 'my_custom_init');
function my_custom_init() 
{
  $labels = array(
    'name' => _x('Books', 'post type general name'),
    'singular_name' => _x('Book', 'post type singular name'),
    'add_new' => _x('Add New', 'book'),
    'add_new_item' => __('Add New Book'),
    'edit_item' => __('Edit Book'),
    'new_item' => __('New Book'),
    'view_item' => __('View Book'),
    'search_items' => __('Search Books'),
    'not_found' =>  __('No books found'),
    'not_found_in_trash' => __('No books found in Trash'), 
    'parent_item_colon' => ''
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','author','thumbnail','excerpt','comments')
  ); 
  register_post_type('book',$args);
}

//add filter to insure the text Book, or book, is displayed when user updates a book 
add_filter('post_updated_messages', 'book_updated_messages');
function book_updated_messages( $messages ) {
  global $post, $post_ID;

  $messages['book'] = array(
    0 => '', // Unused. Messages start at index 1.
    1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Book updated.'),
    /* translators: %s: date and time of the revision */
    5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Book saved.'),
    8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'),
      // translators: Publish box date format, see http://php.net/date
      date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );

  return $messages;
}

//display contextual help for Books
add_action( 'contextual_help', 'add_help_text', 10, 3 );

function add_help_text($contextual_help, $screen_id, $screen) { 
  //$contextual_help .= var_dump($screen); // use this to help determine $screen->id
  if ('book' == $screen->id ) {
    $contextual_help =
      '<p>' . __('Things to remember when adding or editing a book:') . '</p>' .
      '<ul>' .
      '<li>' . __('Specify the correct genre such as Mystery, or Historic.') . '</li>' .
      '<li>' . __('Specify the correct writer of the book.  Remember that the Author module refers to you, the author of this book review.') . '</li>' .
      '</ul>' .
      '<p>' . __('If you want to schedule the book review to be published in the future:') . '</p>' .
      '<ul>' .
      '<li>' . __('Under the Publish module, click on the Edit link next to Publish.') . '</li>' .
      '<li>' . __('Change the date to the date to actual publish this article, then click on Ok.') . '</li>' .
      '</ul>' .
      '<p><strong>' . __('For more information:') . '</strong></p>' .
      '<p>' . __('<a href="http://codex.wordpress.org/Posts_Edit_SubPanel" target="_blank">Edit Posts Documentation</a>') . '</p>' .
      '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>' ;
  } elseif ( 'edit-book' == $screen->id ) {
    $contextual_help = 
      '<p>' . __('This is the help screen displaying the table of books blah blah blah.') . '</p>' ;
  }
  return $contextual_help;
}
?>

Change Log

Source File

register_post_type() is located in wp-includes/post.php.

Resources

Plugin Resources

Related

Post Types: register_post_type(), add_post_type_support(), remove_post_type_support(), post_type_supports(), post_type_exists(), set_post_type(), get_post_type(), get_post_types(), get_post_type_object(), get_post_type_capabilities(), get_post_type_labels(), is_post_type_hierarchical(), is_post_type_archive(), post_type_archive_title()

See also index of Function Reference and index of Template Tags.