Create or modify a post type. register_post_type should only be invoked through the 'init' action. It will not work if called before 'init', and aspects of the newly created or modified post type will work incorrectly if called later.

Note: You can use this function in themes and plugins. However, if you use it in a theme, your post type will disappear from the admin if a user switches away from your theme.

register_post_type() 描述


<?php register_post_type$post_type$args ); ?>

register_post_type() 用法



(string) (必填) Post type. (max. 20 characters, cannot contain capital letters or spaces)

默认值: None


(array) (可选) An array of arguments.

默认值: None



(string) (可选) A plural descriptive name for the post type marked for translation.

默认值: $post_type


(array) (可选) labels - An array of labels for this post type. By default, post labels are used for non-hierarchical post types and page labels for hierarchical ones.

默认值: if empty, 'name' is set to value of 'label', and 'singular_name' is set to value of 'name'.

  • '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'.
  • 'menu_name' - the menu name text. This string is the name to give menu items. Defaults to a value of 'name'.
  • 'name_admin_bar' - name given for the "Add New" dropdown on admin bar. Defaults to 'singular_name' if it exists, 'name' otherwise.
  • 'all_items' - the all items text used in the menu. Default is the value of 'name'.
  • 'add_new' - the add new text. The default is "Add New" for both hierarchical and non-hierarchical post 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. In the UI, this label is used as the main header on the post's editing panel. The default is "Edit Post" for non-hierarchical and "Edit Page" for hierarchical post types.
  • 'new_item' - the new item text. Default is "New Post" for non-hierarchical and "New Page" for hierarchical post types.
  • '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 is used only in hierarchical post types. Default is "Parent Page".


(string) (可选) A short descriptive summary of what the post type is.

默认值: blank


(boolean) (可选) Controls how the type is visible to authors (show_in_nav_menus, show_ui) and readers (exclude_from_search, publicly_queryable).

默认值: false

  • 'true' - Implies exclude_from_search: false, publicly_queryable, show_in_nav_menus, and show_ui. The built-in types attachment, page, and post are similar to this.
  • 'false' - Implies exclude_from_search: true, publicly_queryable: false, show_in_nav_menus: false, and show_ui: false. The built-in types nav_menu_item and revision are similar to this. Best used if you'll provide your own editing and viewing interfaces (or none at all).

If no value is specified for exclude_from_search, publicly_queryable, show_in_nav_menus, or show_ui, they inherit their values from public.


(boolean) (可选) Whether queries can be performed on the front end as part of parse_request().

默认值: value of public argument

Note: The queries affected include the following (also initiated when rewrites are handled)

  •  ?post_type={post_type_key}
  •  ?{post_type_key}={single_post_slug}
  •  ?{post_type_query_var}={single_post_slug}

Note: If query_var is empty, null, or a boolean FALSE, WordPress will still attempt to interpret it (4.2.2) and previews/views of your custom post will return 404s.


(boolean) (可选) Whether to generate a default UI for managing this post type in the admin.

默认值: 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

Note: _built-in post types, such as post and page, are intentionally set to false.


(boolean) (可选) Whether post_type is available for selection in navigation menus.

默认值: value of public argument


(boolean or string) (可选) Where to show the post type in the admin menu. show_ui must be true.

默认值: value of show_ui argument

  • 'false' - do not display in the admin menu
  • 'true' - display as a top level menu
  • 'some string' - If an existing top level page such as 'tools.php' or 'edit.php?post_type=page', the post type will be placed as a sub menu of that.

Note: When using 'some string' to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top-level link. If this isn't desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower.

Note: As this one inherits its value from show_ui, which inherits its value from public, it seems to be the most reliable property to determine, if a post type is meant to be publicly useable. At least this works for _builtin post types and only gives back post and page.


(boolean) (可选) Whether to make this post type available in the WordPress admin bar.

默认值: value of the show_in_menu argument


(string or array) (可选) The string to use to build the read, edit, and delete capabilities. 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') the first array element will be used for the singular capabilities and the second array element for the plural capabilities, this is instead of the auto generated version if no array is given which would be "storys". The 'capability_type' parameter is used as a base to construct capabilities unless they are explicitly set with the 'capabilities' parameter. It seems that `map_meta_cap` needs to be set to true, to make this work.

默认值: "post"

Some of the capability types that can be used (probably not exhaustive list):

  • post (default)
  • page

These built-in types cannot be used:

  • attachment
  • mediapage

Note: If you use capabilities parameter, set capability_type to an empty string to avoid it make interferences with others capabilities.


(array) (可选) An array of the capabilities for this post type.

默认值: capability_type is used to construct

By default, seven keys are accepted as part of the capabilities array:

  • edit_post, read_post, and delete_post - These three are meta capabilities, which are then generally mapped to corresponding primitive capabilities depending on the context, for example 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.
  • publish_posts - Controls publishing objects of this post type.
  • read_private_posts - Controls whether private objects can be read.

Note: those last four primitive capabilities are checked in core in various locations.

There are also eight 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. 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).

  • read - Controls whether objects of this post type can be read.
  • delete_posts - Controls whether objects of this post type can be deleted.
  • 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.
  • create_posts - Controls whether new objects can be created

Note: In fact, when some user have a role with just the post type capabilies it isn't enough for create new object... The rôle must have edit_posts capability to add objects... The matter is that make to appear posts, comments and tools menus...

If you assign a 'capability_type' and then take a look into the $GLOBALS['wp_post_types']['your_cpt_name'] array, then you'll see the following:

Note the "s" at the end of plural capabilities.


(boolean) (可选) Whether to use the internal default meta capability handling.

默认值: null

Note: If set it to false then standard admin role can't edit the posts types. Then the edit_post capability must be added to all roles to add or edit the posts types.


(boolean) (可选) Whether the post type is hierarchical (e.g. page). Allows Parent to be specified. The 'supports' parameter should contain 'page-attributes' to show the parent select box on the editor page.

默认值: false

Note: this parameter was planned for Pages. Be careful, when choosing it for your custom post type - if you are planning to have many entries (say - over 100), you will run into memory issue. With this parameter set to true WordPress will fetch all entries of that particular post type, together with all meta data, on each administration page load for your post type.


(array/boolean) (可选) An alias for calling add_post_type_support() directly. As of 3.5, boolean false can be passed as value instead of an array to prevent default (title and editor) behavior.

默认值: 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' (menu order, hierarchical must be true to show Parent option)
  • 'post-formats' add post formats, see Post Formats

Note: When you use custom post type that use thumbnails remember to check that the theme also supports thumbnails or use add_theme_support function.


(callback ) (可选) Provide a callback function that will be called when setting up the meta boxes for the edit form. The callback function takes one argument $post, which contains the WP_Post object for the currently edited post. Do remove_meta_box() and add_meta_box() calls in the callback.

默认值: None


(array) (可选) An array of registered taxonomies like category or post_tag that will be used with this post type. This can be used in lieu of calling register_taxonomy_for_object_type() directly. Custom taxonomies still need to be registered with register_taxonomy().

默认值: no taxonomies


(boolean or string) (可选) Enables post type archives. Will use $post_type as archive slug by default.

默认值: false

Note: Will generate the proper rewrite rules if rewrite is enabled. Also use rewrite to change the slug used.


(boolean or array) (可选) Triggers the handling of rewrites for this post type. To prevent rewrites, set to false.

默认值: true and use $post_type as slug

$args array

  • 'slug' => string Customize the permalink structure slug. Defaults to the $post_type value. Should be translatable.
  • 'with_front' => bool Should the permalink structure be prepended with the front base. (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/). Defaults to true
  • 'feeds' => bool Should a feed permalink structure be built for this post type. Defaults to has_archive value.
  • 'pages' => bool Should the permalink structure provide for pagination. Defaults to true
  • 'ep_mask' => const As of 3.4 Assign an endpoint mask for this post type. For more info see Rewrite API/add_rewrite_endpoint, Trac Ticket 19275, and this Make WordPress Plugins summary of endpoints.
    • 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

Note: If registering a post type inside of a plugin, call flush_rewrite_rules() in your activation and deactivation hook (see Flushing Rewrite on Activation below). If flush_rewrite_rules() is not used, then you will have to manually go to Settings > Permalinks and refresh your permalink structure before your custom post type will show the correct structure.


(boolean or string) (可选) Sets the query_var key for this post type.

默认值: true - set to $post_type

  • 'false' - Disables query_var key use. A post type cannot be loaded at /?{query_var}={single_post_slug}
  • 'string' - /?{query_var_string}={single_post_slug} will work as intended.

Note: The query_var parameter has no effect if the ‘publicly_queryable’ parameter is set to false. query_var adds the custom post type’s query var to the built-in query_vars array so that WordPress will recognize it. WordPress removes any query var not included in that array.

If set to true it allow you to request a custom posts type (book) using this:

If set to a string rather than true (for example ‘publication’), you can do:


(boolean) (可选) Can this post_type be exported.

默认值: true


(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

默认值: 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)


(object | WP_Error

The registered post type object, or an error object.

register_post_type() 返回值


  • Note that although the $public attribute is optional, the inputs passed to the register_post_type() function are exactly what is queried by the get_post_types() function. So if you verbosely set the equivalent options for publicly_queriable, show_ui, show_in_nav_menus, and exclude_from_search, this will not be handled the same as if you had set the $public attribute. See bug 18950.

register_post_type() 注意


  • 3.5.0: 'supports' argument can be set to Boolean false to disable default title and editor.
  • 添加于 版本 2.9

register_post_type() 历史


register_post_type() 函数的代码位于 wp-includes/post.php.

register_post_type() 源文件