Making Custom Post Types and Formats

in WordPress Tutorials

As a theme or plugin developer, you may want to incorporate the ability to make WordPress even more of a content management system. Two major methods of doing just that are making custom post types and custom post formats. These two are important due to the flexibility and increased organization that adding a new post type or format can offer you and your software’s users. This tutorial for developers will go into depth about what each method is, what it can do, and how you use it.

Custom Post Type

MCPTaF typeA post type is a method of organizing content in a specific category in a specific manner. Yes, you could create a category for posts about a specific subject, then make a single.php just for that category’s posts. What a post type allows you to do that a category does not is that post types allow you to create another menu in the sidebar just for this post type, enables you to exclude this type of post from searches, and even gives you the option to make this post type private, among several other things.

By default, WordPress creates five separate post types. The default post types are: post, page, attachment, revision, and nav_menu_item. These five drive the basic functionality that makes WordPress into a great blogging platform. A post’s type is saved in a separate column within the same wp_posts table, making it easier to perform queries to filter specific post types. The “post” type is for content that will be updated and added upon in the future. A “page” is for static content that should contain non-volatile information. A “revision” is a child of a post or page that contains an older version of that post or page’s content. An “attachment” is a media file that is linked to by a post or page and is uploaded via the Media upload system. Finally, a “nav_menu_item” is a single item contained within a menu, and can be a link to any page, category, post, or an external resource.

Note that the five default post types do not use a namespace before the type of post, but it has been suggested that “wp_” will be used in the future to match WordPress’ core naming scheme, so you will not be allowed to use that namespace in the future. Keeping this in mind, when making a new post type, use the namespace of your own plugin or theme in order to prevent naming conflicts.
An example use of a custom post type would be for a website that should display a list of events. A theme or plugin could create a custom post type called “Events” and add a new meta box for the event information. That way, blog posts can be kept separate from events in the eyes of the visitors and authors. It also makes creating the theme or plugin easier because you can target a specific post type when using add meta box and when making template files (by making a “single-$postype.php” file).

Making a Post Type

Knowing more about custom post types and what they are, you can proceed to creating a custom post type. The function that enables you to create a custom post type is register_post_type, which accepts the new post type and an optional array of arguments.

The usage of register_post_type is easy and complicated at the same time, for it offers many options for you to set, but many of them are defaulted to what is minimally needed. The basic usage is shown in the code below.

The argument “my_post_type” is the slug of your post type. The array in the second argument will carry all of the options that you intend to set. Some of the available options are:

  • label – a name for your post status that is plural and enclosed in “_x()” for translation
  • description – A summary of what this post type is for.
  • public – If the post type should be displayed in wp-admin and on the front-end. This option is used to set the default of several other options.
  • exclude_from_search – When a visitor searches a site, should this post type be excluded in the list of results?
  • show_ui – Whether or not this post type should get a management interface in wp-admin
  • menu_position – Where to show the menu item for the UI in wp-admin’s sidebar. Three of the many possibilities are“5,” for just below the Posts menu, “25,” for just below the Comments menu, and “100,” for just below the second separator.
  • menu_icon – The picture icon next to the text for the menu item. WordPress will display the same icon as the Posts menu by default.
  • hierarchical – If there will be a parent-child relationship between different posts within this post type, this should be set as true.
  • supports – What this post type should handle, and what functions should be assigned to it. The list is: ‘title’, ‘editor’, ‘author’, ‘thumbnail’, ‘excerpt’, ‘trackbacks’, ‘custom-fields’, ‘comments’, ‘revisions’, ‘page-attributes’, and ‘post-formats’
  • has_archive – If this post type should have its own archives. The archive URL will use either its slug or the option set in “rewrite”
  • rewrite – Either a string containing just the intended slug or an array of intended slugs for various permastruct uses.

If you choose to create your own rewrite rule, you will need to flush the rewite rules by calling flush_rewrite_rules() int your plugin’s activation hook or your theme’s switch hook, but only after registering your custom post type. More information can be found in the WordPress Codex.

When creating your custom post type, you should check to see if it already exists using post_type_exists before you call register_post_type. If the post type does not exist, then you can create it as you wish. However, if it already exists, your theme or plugin still has the ability to alter the post type’s properties using add_post_type_support and remove_post_type_support. To see a full list of what a post type can support, see the documentation on either of these functions.
Combining what we have covered above, your code may appear similar to the code below. It checks to ensure that your code does not register the same post type twice, and hooks a function into “init” to either register the post type or to add a capability that we need to the post type to have.

Custom Post Format

MCPTaF formatA form of taxonomy, post formats are used to customize the display of posts based on their user-selected format. While a theme developer cannot create a separate template file for each post format, the developer can create the theme to discover a post’s format and alter the display of that post based on that format.

An example use of a post format would be on a political website that covers multiple people. Each person would receive their own category, and different types of content would be different post formats. If done in this fashion, visitors could subscribe to the RSS feed of their favorite candidate, and allows the website to use unique CSS styles when differentiating between the various content formats.

Making a Post Format

WordPress makes it easy for developers to create new post formats. All a theme or plugin developer has to do is to call add_theme_support in a fashion similar to the code below.

If you want to use post formats with a custom post type, you will need to include “post-formats” in the “supports” array within register_post_type, or use code similar to “add_post_type_support( ‘my_post_type’, ‘post-formats’ );“ Either one will accomplish the same task, but the first option should be used if your code creates the post type, while the second option should be used if the post type is already defined. The code at the end of the previous section demonstrates this by use of the “if/else” statement.

To find if the current post has a certain format, you can call has_post_format($format) in an “if” statement. If you want to use a “switch” statement, you can call get_post_format() to receive the current post’s format. If your theme uses the post_class function within the post wrapper, WordPress will also add another class using the “format-$format” class name. These will allow your theme or plugin to customize what appears based on the current format being displayed.


While this is a short list of methods to customize the editing and display of content, these are two great methods. Both custom post types and formats make it easier for websites powered by WordPress to more effectively manage, organize, and display their content. If you have other great methods of making WordPress a better content management system, write them down in the comment section below for others to find and discuss.


Comments (2)

  • Comment by webpage

    My coder is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the
    costs. But he’s tryiong none the less. I’ve been using Movable-type on a number
    of websites for about a year and am concerned about switching to another platform.
    I have heard very good things about
    Is there a way I can import all my wordpress posts into it?
    Any kind of help would be really appreciated!

  • Comment by Rami

    Or you can use to this automatically.