Amber Weinberg: Freelance Web Developer specializing in semantic WordPress, Mobile, CSS and HTML5 Development

The Blog

Working With WordPress 3.0′s Custom Post Types

Posted on 01/06/11 in blog, development about , , ,

Just last week I finally got the chance to try the new custom post types that came with WordPress 3. For some reason, I never thought about using them because I was so used to using custom fields for everything. After setting up some pre-made templates for a few clients that used custom post types, I decided to check it out and try them for myself.

It turns out custom post types are easy to setup, understand and have made my code MUCH cleaner! I learned a lot about this new function in just a short time, so I thought I’d share it with everyone.

Setting Up Custom Post Types

Before you can do anything, you have to first set up your custom post type. In your functions.php file you need to add:

register_post_type('praise', array(
	'label' => __('Praise'),
	'singular_label' => __('Praise'),
	'public' => true,
	'show_ui' => true,
	'capability_type' => 'post',
	'hierarchical' => false,
	'rewrite' => false,
	'query_var' => false,
	'supports' => array('title', 'editor', 'author'),
));

This sets up a custom post type called “praise” and will add a new section to the top of the admin navigation area called “Praise”. From there, you can begin to add new posts.

Adding Custom Fields

The current setup above allows you to add new content, much like the regular “Posts” section does. However, right now it doesn’t allow you to input custom fields, which hinders our ability to truly customize our design.

Luckily, WordPress allows us to add more sections to our custom post type, placed in the “supports” array. You can add your taxonomies, versions and more. To add custom fields, simply add this to the supports array:

register_post_type('praise', array(
	'supports' => array('title', 'editor', 'author','custom-fields'),
));

Using Custom Post Types In A Query

The next thing I needed to do was to actually display the posts in a query loop so I could list them on a page, instead of an archive page. This was actually really easy to go with the standard ‘query_posts’ function:

<?php query_posts('post_type=praise'); if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

Simply change “praise” to whatever you named your custom post type in function.php file.

Giving Custom Post Types Pretty Permalinks

The next problem I encountered were the links that were created with custom post types. Unfortunately, it doesn’t follow WordPress’s permalink structure, and comes up with an ugly and HTML5/XHTML1.0 invalid use of ampersands in the URL.

With the addition of a few more lines to our register_post_type function though, we can add pretty permalinks:

register_post_type('praise', array(
	'rewrite' => array(
    	'slug' => 'praise',
    	'with_front' => FALSE,
  ),
));

You can change the slug name to be whatever you want the proceeding folder to be in the URL.

Now, this changes all the actually URLs on the site, but if you try to click on them, it will actually take you to the 404 error page. So after our register_post_type function, we need to add:

global $wp_rewrite;
$wp_rewrite->flush_rules();

Which will flush our rewrite rules and will allow the new permalinks to work.

Custom Templates

What’s cool about custom post types, is that you’re not limited to the normal single.php file for templating. If you want to use a custom template, simply name it single-praise.php (or whatever name you chose for yours) and WordPress will automatically pick it up and use that file instead.

Want To See An Example?

Check out one of my client’s current site in progress to see the use of custom post types (in praise and pictures sections).

If only I had time to recode my own site now :( .

The Power

What makes custom post types REALLY powerful is when you combine it with custom taxonomies, custom fields and the rest of WordPress’s CMS functions. Every site I code works completely different from the previous one, and I’m hoping with these new features my sites can finally be somewhat similar in the bones. :)

About the author
Amber Weinberg specializes in clean and semantic XHTML, CSS and WordPress development. She has over 10 years of coding experience and is pretty cool to work with. Amber is available for freelance work, so why not hire her for your next project?

19 Awesome Comments

  1. Excellent intro for devs, Amber. I haven’t played around too much with custom post types yet but I have some projects coming up with it. Thanks for the roundup.

  2. Very cool! Definitely bookmarking this, I’ve been wondering how to do this for quite some time. Great post, Amber!

  3. Miguel Palazzo says:

    Here’s my dime for a fellow dev! Nice tut, I’ve been playing with custom post types for quite a while, and it’s incredibly useful

    • Yea I can’t wait to start a few more sites to play with them more…makes me want to recode my whole site.

      • Miguel Palazzo says:

        I did that for one of my company websites, I loved it :D Had to redo all of the content that was later a custom post type, but hey, it turned out onto something really amazing and easy to edit by the person in charge of content

  4. Lucian says:

    Here is a post with more advance info on the subject:
    http://goo.gl/SIqtC – WordPress custom post types, custom back-end columns and post thumbnails

    I’d also still use the custom fields with the custom post types for even more flexibility. Custom field template plugin it is integrating great.

  5. Great article! I’ve been wanting to play with custom post types for some times now, but haven’t gotten to it for some reason. Seeing how simple it actually is makes me want to play with it more :)

    Thanks!!

  6. AJ Morris says:

    I think this was the most exciting feature I was looking forward to in 3.0! I’ve used them on a few sites, and I’m finding it easier and easier to just use these instead of a “post” with custom fields attached.

  7. Great read, found some other resources online about this, read the codex and am excited to get it working on a few of my own sites. This is such a powerful feature and adds a whole new level of flexibility in the way content can be added and organized. One cool way that I’ll be using it is to allow registered users to add to a custom post type themselves to build up a directory. Thanks again!

  8. Yari says:

    Excellent intro to custom post types. I haven’t played around with them yet but definitely will soon! Thanks Amber

  9. Christopher Wulff says:

    Great intro post. Post types, taxonomies and post formats are really giving us great new tools, though they can be confusing.

    I’ve found it really helpful in talking with folks to keep reinforcing thinking about them as custom content types rather than post types. Helps everyone wrap their heads around it a little faster. Also, reminding folks that they’re not included in the RSS by default is important too.

  10. Excellent post! I’m really tempted to use custom post types on my next WP project.

  11. Vladimir says:

    Very good tutorial!

    But flush_rules() function is “quite slow” (according to WP codex) and moreover this shold be done only once, after theme activation.

    You can flush rules by visiting Settings -> Permalinks page.

  12. Will says:

    I wish I had time to change my portfolio too… :( Next week!

Leave a Reply