An Introduction to Custom Post Types in WordPress

Share this post:

If you’ve just started tinkering under the hood of WordPress, you’ve probably come across custom post types or you may have noticed that many themes come with their own custom post types. If you’re wondering what exactly a custom post type is and what is it for, you’re not alone!

In fact, the phrase “custom post types” is a bit of a misnomer – a post type is really just another word for “content” and doesn’t necessarily have to be a post as such. wordpress-custom-post-types WordPress comes with five different post types as default:

  • Post
  • Page
  • Attachment
  • Revision
  • Navigation menu

Since WordPress 3.0, it has also been possible to add your own custom post types with their own appearance, properties and behaviors.

Why Do You Need Custom Post Types?

Once you start using WordPress to build sites other than basic blogs, it won’t be long before the default post types can seem quite restricting. In general, it’s recommended to use “post” for blog posts, news updates and the like, and “page” for static informational pages such as your “About” page and contact information.

This is fine for most users, however if you want to create some new content that doesn’t fit neatly into the category of “post” or “page”, this is where custom post types come in. There are many reasons why you might want to create your own custom post type in WordPress. For example, if you’re building an e-commerce store, it would be logical to create a new “product” post type, rather than using the standard post or page content. Some other examples of content you may wish to develop a custom post type for are:

  • User profiles
  • Reviews
  • Testimonials
  • Resources
  • Events
  • Property listings

There’s really no limit to the uses of custom post types and their power lies in the fact that they are truly “custom” and unique to the requirements of your website. Using custom post types extends WordPress beyond a simple blogging platform and allows it to become a fully-fledged CMS.

How to Create a Custom Post Type

Custom post types can either be created manually by editing the functions.php file of your WordPress installation, or by using a plugin to do the dirty work for you. For manual creation, you need to use the register_post_type() function which enables you to define a new post type and specify labels, features and availability. Here’s a code sample for creating a product post type from the official WordPress Codex:

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'acme_product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
)
);
}

While you can name your new post type anything you like, it’s important to follow the WordPress naming guidelines when using the post creation function. The identifier should be prefixed by a namespace linking it to the plugin, theme or website that will be using the custom post type. This is to prevent any conflicts with other custom post types that may be introduced from themes or plugins you install. In the example above, you can see that the post type has been registered as “acme_product”, rather than just “product”. Be aware that the identifier should not exceed 20 characters and you should not use “wp” as your identifier, as this is reserved for internal WordPress objects and functions.

There’s more information about setting up custom post types manually over at the WordPress Codex. There’s also a handy post type generator available for free use at GenerateWP, which can really speed up your coding time so you simply need to cut and paste the generated code into your functions.php file. Check out the provided examples for some ideas of how it can be used. If you’re not comfortable with using PHP or editing your WordPress files manually, there are several plugins that will make things easier for you. These plugins are all very to use, allowing you to create a new custom post type simply by typing in the name and selecting from various options: custompress-posttypes

  • CustomPress is a premium plugin from the team at WPMUDev and describes itself as the “ultimate plugin for transforming WordPress from a blogging platform into a full blown CMS”. The plugin allows you to easily create a range of post types with their own custom taxonomies, custom fields and other customization options. It also generates the code for you so that you can use it for generating custom post types to use in your own theme files. CustomPress costs $19 or you can join WPMUDev and have access to all 350+ of their plugins for $39.60.
  • Types is a free plugin that allows you to create and manage custom post types and custom fields easily. Types also has some extra handy functionality such as custom fields and the ability to define relationships between different post types. Combined with the other Toolset plugins, it gives you the power to create a completely custom CMS built on WordPress.
  • Pods is a content development framework that includes functionality to create custom post types and fields. There’s also the option to create advanced custom types that are completely separate from the WordPress system. Pods is free to download and use.
  • Custom Post Type UI is another easy to use plugin that creates a graphical interface for you to register custom post types and taxonomies.

All these plugins do a great job at simplifying the job of creating custom post types so it’s down to personal requirements and preference which one will suit you best – I suggest you download two or three and try them out.

Custom Taxonomies

taxonomy Using custom taxonomies is an optional way of categorizing your new custom post types instead of using categories and tags, as you may do in a normal blog post. Both tags and categories are a type of taxonomy but they may not necessarily work for your new content types and this is why you may want to create a new custom taxonomy. For example, you may wish to create a “product” post type for an e-commerce store and then create several custom taxonomies such as “product-type”, “brand”, “material”, “size” etc. Taxonomies can be created using the register_taxonomy() function in your functions.php file. Here’s an example for creating a “people” taxonomy:

function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides'
)
)
);
}
add_action( 'init', 'people_init' );

most of the aforementioned plugins will also allow you to create custom taxonomies from the interface, or you can use a plugin like Simple Taxonomy.

Custom Post Type Templates

Creating a custom post either manually or via one of these plugins will only set up the backend structure of the custom content. To control the way each of your post types looks, you will also need to create a template. WordPress comes with several templates to display the default post types in different ways. For example, the default post type can look different depending on whether it is viewed with the “single” post template or the “archive” post template. New custom post types will also use these template files as default unless you create a different one to use.

To create a new template file for your new custom post type, simply create a new file called single-posttype.php where posttype is the identifier of the custom post (for example, “single-acme_product.php” using the example above). If WordPress does not find a template file matching this name, it will default to the normal single post template. If you’re new to creating custom page templates, you’ll probably find it easiest to start with the code from the default single.php template and edit it to suit your needs. The WordPress Codex has all the information you need to know about creating and using templates within WordPress.

Conclusion

As you start to understand the power of custom post types, you can see how WordPress can easily be converted from a basic blogging system to a complex custom CMS. Using custom post types, taxonomies and templates in your WordPress themes and sites gives you power and flexibility to create whatever kind of site you like. Have you tried out any of the plugins mentioned above? Any other tips for working with custom post types? Please share them in the comments!

Image credits: Ian Ruotsala

About the Author: Rachel is a web-developer-turned-writer who has successfully convinced many clients to switch to WordPress over the years. She blogs about her life as an expat in Bali at How to Escape.Get more from Rachel on  and Twitter.
3 comments… add one
  1. Jody Raines says

    Thanks for breaking this down and making it easy to understand. I can see several ways that I can extend WordPress using a custom type, and especially appreciate your sharing some plugin tools that can help make this easy. Great article.

Leave a Reply

Disclosure: This post may contain affiliate links, meaning that if you click on one of the links and purchase an item, we may receive a commission (at no additional cost to you). All opinions are our own and we do not accept payments for positive reviews.