Web Analytics

Custom Post Types: Manage Your Content Better in WordPress

by Jason Unger, Founder

Photo by Chris Benson via Unsplash

I say the same shtick every time when I’m explaining why we use and recommend WordPress: it’s flexible, so you can build whatever you need, without having to overbuild anything.

Given its history as a blog platform and not a “full-featured” content management system (CMS), it’s not a surprise that WordPress generally breaks down content into two different types:

  • Posts (Date-based content that can be categorized and tagged)
  • Pages (Evergreen content that can have parent or child pages)

It’s straight forward and simple.

But what do you do when you have content that isn’t necessarily date-based, but needs to be categorized? Or when you have date-based content but isn’t your typical blog post or news item?

Enter Custom Post Types.

Custom Post Types: Silos for Your Content

WordPress allows you to create custom post types, which are treated as their own type of content, separate and apart from your pages and posts.

Here’s the official definition of them, from WordPress.org:

Whilst there are already lots of standard post types within WordPress, you may want to extend the amount of post types you have if you want to break things down into smaller categories. For example, if you want to have a section on Books, it would be better suited to creating a custom post type for them.

So why would you want to have a specific custom post type just for, say, Books?

Let’s say your website reviews books and includes affiliate links to buy those books on Amazon. Here’s a few reasons why you’d make Books a custom post type:

  • If your site also includes blog posts or press releases, then traditionally you’d use the built-in Posts type for those.
  • While you might be able to create those Books as pages, they’d be organized in the same “bucket” as your About, Contact, Home, etc. pages even though they are different types of content.
  • For Books, you may have specific content needs, like an image of the cover, custom fields for the author and affiliate link, etc. Your ‘normal’ pages don’t have that, so it wouldn’t make sense to group them together.
  • You may want to create custom categories (taxonomies) for the books, based on genre, audience, and more.

You can extend this logic out to any type of content you have with specific needs that differ from traditional posts or pages. Custom post types allow you to better organize your content, without needing to jerry-rig your existing content types.

How to Register Custom Post Types

Thankfully, it’s very easy to create and manage your custom post types.

Not surprisingly, there are a number of WordPress plugins that will do it for you, including the Custom Post Type UI and Custom Post Type Maker plugins.

But as you know, we focus on minimalist code in our website development, so our preferred method is using the register_post_type function in WordPress.

Here’s what it looks like to register two custom post types in a typical functions.php file.

/*Custom Post Types*/
add_action( 'init', 'create_cpts' );
function create_cpts() {
  register_post_type( 'staff',
    array(
      'labels' => array(
        'name' => __( 'Staff' ),
        'singular_name' => __( 'Staff Member' ),
        'add_new_item' => __( 'Add New Staff Member' )
      ),
      'public' => true,
      'menu_icon' => 'dashicons-businessperson',
      'rewrite' => array('slug' => 'staff'),
      'menu_position' => 20,
      'supports' => array( 'title', 'editor', 'author','thumbnail' )
    )
  );
  register_post_type( 'resources',
    array(
      'labels' => array(
        'name' => __( 'Resources' ),
        'singular_name' => __( 'Resource' ),
        'add_new_item' => __( 'Add New Resource' )
      ),
      'public' => true,
      'menu_icon' => 'dashicons-portfolio',
      'rewrite' => array('slug' => 'resource'),
      'menu_position' => 20,
    'supports' => array( 'title', 'editor', 'author','thumbnail','excerpt' )
    )
  );
}

Reviewing this code, all of your custom post types must or should have:

  • a type (‘staff’ or ‘resources’) in this example
  • labels/names
  • visibility/accessibility via the dashboard and front-end
  • a menu icon from a WordPress Dashicon
  • what the permalink/URL for the post type should be (rewrite)
  • where in the backend menu the custom post type should go
  • the features the post type supports

(The entire description of how to use register_post_type() can be found in the WordPress Developer Resources)

Once you register your custom post type, you’ll see it appear in the backend of your WordPress site, and then you can create away!

Important: it is often the case that, after creating a custom post type, you’ll get an error when trying to view your content on the front-end for the first time. If this happens, don’t worry! Go to Settings -> Permalinks and click ‘Save’. Try again, and it’ll work!

Advanced Custom Post Type Tips + Tricks

Looking to extend your custom post types even further? Try these on for size.

If you want to organize your custom post types beyond the traditional Categories and Tags, you can create Custom Taxonomies.

Let’s say you wanted to have different ‘Types’ of Resources so you can easily filter. Here’s how you would do that.

/*Custom Post Type Taxonomy*/
add_action( 'init', 'create_taxs' );
function create_taxs() {
	register_taxonomy(
		'types',
		'resources',
		array(
			'labels' => array(
		        'name' => __( 'Types' ),
		        'singular_name' => __( 'Type' ),
		        'add_new_item' => __( 'Add New Type' ),
		        'edit_item' => __( 'Edit Type' ),
		        'update_item' => __( 'Update Type' ),
		        'new_item_name' => __( 'New Type' ),
		        'search_items' => __( 'Search Types' ),
		        'not_found' => __( 'No Type found.' ),
		        
		      ),
		 'hierarchical' => true,
	         'rewrite' => array( 'slug' => 'type' ),
		 'public' => true,
                 'show_admin_column' => true,
		)
	);
}

What if you wanted to use an icon from the amazing Font Awesome library for your custom post type?

You can make that happen by loading the Font Awesome stylesheet and targeting CSS to replace the default icon. Here’s how we did that for our clients at Silver Branch Brewing, who have a custom post type for their beers.

function fontawesome_dashboard() {
     wp_enqueue_style('fontawesome', 'https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css', '', '4.0.3', 'all');
}
add_action('admin_init', 'fontawesome_dashboard');

function fontawesome_icon_dashboard() {
     echo "<style type='text/css' media='screen'>
          #adminmenu .menu-icon-beers div.wp-menu-image:before {
          font-family: Fontawesome !important;
          content: '\\f0fc';
     }
     </style>";
}
add_action('admin_head', 'fontawesome_icon_dashboard');

There’s plenty that you can do with Custom Post Types, and I highly encourage you to use them. It makes organizing your content so much easier, and you’ll never feel like you’re bending over backwards to get something to work the way it should.

Have a question about how your content is organized? Reach out to us and we’ll be happy to help.

Avatar photo
About Jason Unger

Jason Unger is the Founder of Digital Ink. He built his first website on Geocities, and hasn't looked back since. Digital Ink tells stories for forward-thinking businesses, mission-driven organizations, and marketing and technology agencies in need of a creative and digital partner.

Other Stories You May Like

What’s your story?

Let’s share it with the world.

Let’s Do This

Close Window
All the Cool Kids are Doing it

Sign Up for the Digital Ink Newsletter

All the cool kids are doing it.