You’re a WordPress developer. Your client has a well-defined need for custom website content. You have a pretty decent idea how to deliver what is required. How do you get started?

Should you build a custom page template with custom fields, or would you be better off using custom post types?

This blog post will go through the ins and outs of both options. We’ll also discuss a couple of popular WordPress plugins that make the task considerably easier while providing lots of other useful functionality — Advanced Custom Fields by Eliot Condon, and Types from OnTheGoSystems.

Advanced Custom Fields is great if you need to create simple custom page layouts. Types is better if you need full-blown custom post types with their own layout, styling, taxonomies, and archive pages.

This happened:

I recently built a custom page template using custom fields. I’m using the same template for two different pages. The template requires a repeating field that collects 9 separate pieces of input to send to the front-end. One page has about 25 repeating elements. It works great.

The other page has already crashed. Sometime around adding the 71st repeating element, white screen of death. We’re talking 71 x 9 = 639 separate variables. The admin edit page exceeds the server-imposed memory limit, resulting in the dreaded white screen of death. Not only that, but it also makes for a rather cluttered admin edit page:

fields-adminI wouldn’t want to have to make sense of this mess.

Of course, I could just increase the WP_MEMORY_LIMIT in my wp-config file, but that doesn’t really address the issue — I shouldn’t have that many repeating fields in a page template, I should make a custom post type.

It’s one thing if you need a single repeating field on a page. You can build a simple page template that will serve your needs nicely I’m sure. But, if you’re trying to have several input fields inside a single repeater, you probably want to go with a custom post type.

“Sometimes the questions are complicated and the answers are simple.” -Dr. Seuss

Dr. Seuss is right. We in the development world are always searching for beautifully simple answers to complex problems. Since WordPress 3.0, we have had the ability to create our own custom post types in WordPress. This simple tool gives us the ability to make WordPress do pretty much anything we want it to.

It’s helpful to understand how to create your own custom post types, and the WordPress codex is a perfect place to read about it. However, instead of writing yet another How-To-Build-Your-Own-WordPress-Custom-Post-Type post, I’m going to utilize the Types plugin (Free of charge) by OnTheGoSystems.

Types gives you the ability to easily create custom post types, custom fields, categories, tags, and your own custom taxonomies. You can also customize menu position, the url to your single and archive pages, indicate whether to show or hide the post type in the admin and navigation menus, and several other useful options. In order to access the custom content, you can either use Types’ companion plugin, Views (currently $95), or write a little PHP and hook into the Types API (Free).

After some pretty straight forward configuring, I’m able to create a new custom post type, Customer, and several custom fields specific to the post type. My admin edit page interface is much less cluttered, and now I have a lot more control over both content and presentation.

types-adminOh yeah. That’s much better.

One of the great things about WordPress is that you can create custom page templates for a specific post as well as the archive for the post type, but you don’t have to.

When displaying your custom post type, WordPress auto-magically looks in the theme folder for a file called single-{$post-type}.php, and for your custom post type archive page WordPress looks for archive-{$post-type}.php — in my case it would be single-customer.php, and archive-customer.php. If those pages don’t exist, WordPress uses single.php and archive.php. And if those pages don’t exist, you’re gonna get a 404. These pages are where you write some PHP and tie in to the Types API to retrieve those values from all those custom fields (unless you can afford $95).

I’ve gone ahead and created those pages, and now I’m ready for the big finish. To view a real-world example of my custom post type, visit the VictorOps Customers Page.