Posted in WordPress

Create a custom post type in WordPress

Create a custom post type in WordPress Posted on December 26, 2015Leave a comment

WordPress has five built-in post types, they are post, page, attachment, revision and nav_menu_item (navigation menu) but in some cases, we have to create our custom post type for specific reasons. The function below will support us to create a custom post type in WordPress:

  • $post_type: (string) (required) Post type.
  • $args: (array) (optional) An array of arguments.
    • label: (string) (optional) A plural descriptive name for the post type marked for translation.
    • labels: (optional) 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.
      • name: general name for the post type, usually plural.
      • singular_name: name for one object of this post type.
      • menu_name: the menu name text. This string is the name to give menu items.
      • name_admin_bar: name given for the “Add New” dropdown on admin bar.
      • all_items: the all items text used in the menu.
      • add_new: the add new text.
      • add_new_item: the add new item text.
      • edit_item: the edit item text. In the UI, this label is used as the main header on the post’s editing panel.
      • new_item: the new item text.
      • view_item: the view item text.
      • search_items: the search items text.
      • not_found: the not found text.
      • not_found_in_trash: the not found in trash text.
      • parent_item_colon: the parent text. This string is used only in hierarchical post types.
    • description: (string) (optional) A short descriptive summary of what the post type is.
    • public: (boolean) (optional) Controls how the type is visible to authors (show_in_nav_menus, show_ui) and readers (exclude_from_search, publicly_queryable).
    • exclude_from_search: (boolean) (importance) Whether to exclude posts with this post type from front end search results.
    • publicly_queryable: (boolean) (optional) Whether queries can be performed on the front end as part of parse_request().
    • show_ui: (boolean) (optional) Whether to generate a default UI for managing this post type in the admin.
    • show_in_nav_menus: (boolean) (optional) Whether post_type is available for selection in navigation menus.
    • show_in_menu: (boolean or string) (optional) Where to show the post type in the admin menu.
    • show_in_admin_bar: (boolean) (optional) Whether to make this post type available in the WordPress admin bar.
    • menu_position: (integer) (optional) The position in the menu order the post type should appear
      • 5: below Posts
      • 10: below Media
      • 15: below Links
      • 20: below Pages
      • 25: below comments
      • 60: below first separator
      • 65: below Plugins
      • 70: below Users
      • 75: below Tools
      • 80: below Settings
      • 100: below second separator
    • menu_icon: (string) (optional) The url to the icon to be used for this menu or the name of the icon from the iconfont. See more icon in this link.
    • capability_type: (string or array) (optional) 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.
    • capabilities: (array) (optional) An array of the capabilities for this post type.
    • map_meta_cap: (boolean) (optional) Whether to use the internal default meta capability handling.
    • hierarchical: (boolean) (optional) Whether the post type is hierarchical (e.g. page). Allows Parent to be specified.
    • supports: (array/boolean) (optional) An alias for calling add_post_type_support() directly.
      • 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
    • register_meta_box_cb: (callback ) (optional) 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.
    • taxonomies (array) (optional) 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().
    • has_archive: (boolean or string) (optional) Enables post type archives. Will use $post_type as archive slug by default.
    • rewrite: (boolean or array) (optional) Triggers the handling of rewrites for this post type. To prevent rewrites, set to false.
    • query_var: (boolean or string) (optional) Sets the query_var key for this post type.
    • can_export: (boolean) (optional) Can this post_type be exported.
    • show_in_rest: (boolean) (optional) Whether to expose this post type in the REST API.
    • rest_base: (string) (optional) The base slug that this post type will use when accessed using the REST API.
    • rest_controller_class: (string) (optional) An optional custom controller to use instead of WP_REST_Posts_Controller. Must be a subclass of WP_REST_Controller.
    • _builtin: (boolean) (not for general use) Whether this post type is a native or “built-in” post_type.
    • _edit_link: (boolean) (not for general use) Link to edit an entry with this post type.

And after built the function that creates a custom post type, we have to hook it to init action. Here is a basic example:

This article is very basic but I hope you like it. Thanks!

 

Leave a Reply

Your email address will not be published. Required fields are marked *