Using Custom Post Types

When using WordPress, you’re not limited to just Posts and Pages. Those are simply the default post types, and if you want more you can define your own.

I recently launched a site for a client called Flatbooks, which sells eBooks online, and the feature I’d like to highlight how you’d set up your own ebook custom post type.

Registering the Post Type

Registering a custom post type is pretty easy. For all the documentation, please see the Codex.

If you need help writing the code, here is a Custom Post Type Generator that will let you fill out a form to generate the proper code.

As this was an eBook, there were a lot of post meta boxes that weren’t needed (Excerpt, Trackbacks…). But, there was some additional information that would be needed. So the next step was to add an eBook-specific meta box. Here’s my post on Custom Metaboxes.

Coding with Custom Post Types

In our code above we set 'has_archive' => true. This makes an archive available at yoursite.com/ebook. You can customize this using the archive-ebook.php template file. For single posts, use single-ebook.php.

These techniques allow you to create logical divisions of content and context-specific meta information boxes. It makes it easier for the end user to enter data because they know where to go and what data is needed.

chat20 Comments

  1. Neil says

    Great post. Bill, do you think there is a speed-to-load advantage in adding your own code in custom functions versus these custom post plugins? Or is any advantage out-weighed by their ease of use?
    thanks

    • Bill Erickson says

      That’s a good question, and I’ve never tested both options from a performance standpoint. I do know that once you use the Custom Post Type UI plugin to create the post type, you can remove the plugin and everything still works (it’s only used once to create it). I don’t think there’s a performance hit for using either plugin over hand-coding, but I can’t be sure.

  2. Katarsis says

    Hi, i ´am starting my own bussiness in develope wordpress site (i love this) and i have a question
    do you update plugins in the customers sites? or disable the upg´s annoucement and do not upg never? or only when strongly necessary?

    tks but i don´t know how to treat this topic in the future?

    tks again and sorry my language (i am from uruguay)

    • Bill Erickson says

      I try and minimize the use of plugins, but there are a few that I use often. Examples would be All in One SEO (for sites not built with Thesis or Genesis), Contact Form 7, and Akismet. Since I try to use well-established plugins, I don’t often update them because the updates aren’t (usually) security-based, they only provide new features. If the old plugin is working, no reason to upgrade and take a chance of it breaking.

    • Nanette says

      I tried followed your tutorial, thanks… but I am having a problem. When I pasted this code into my custom_page template I get errors. And I dont see any of the custom post.

      • Bill Erickson says

        What type of error did you get?

        I didn’t provide the complete code for displaying them on the page, so I’m not sure what code you copied.

        • Nanette says

          I guess I need the full code that I would put int customfunctions.php (in Thesis.) Can you help me out?

          I already have custom template that replaces my thesis_custom_template that I use for my home page. Is it possible to use a conditional tag that says if front_page use template “A” and then if product_page (which is my custom post type use template “B”?

    • Bill Erickson says

      Change query_posts to this: query_posts(‘post_type=staff-directory&showposts=-1&orderby=title&order=ASC’);

      That will sort them by the title, from A-Z.

      • says

        Bill-
        I ran across this a bit after posting, but my situation is that I don’t use the default title, I use meta fields name_last_name and name_first_name. So, I need to be able to sort by name_last_name.

        Thanks!
        ~Kyle~

        • Bill Erickson says

          Ah, then you should change query_posts to this:
          query_posts(‘post_type=staff-directory&showposts=-1&meta_key=name_last_name&orderby=meta_value&order=ASC’);

  3. says

    Hi Bill, thanks so much for your great resources (love core-functionality, in particular).

    Just wanted to clarify – you’ve written: “You can customize this using the archive-ebook.php template file. For single posts, use archive-ebook.php.” Should that last line read single-ebook.php?

    I’m was having trouble with the has_archives property – eventually found that I needed to flush the rewrite rules by hitting Save on the Permalink settings. Had me scratching my head – hope it helps someone :)

  4. Jamie says

    Dear Bill

    i’m using a custom post type of ‘portfolio’ and the genesis framework.

    the portfolio will be the home page (for a designers site so the latest work is up front)

    i created a function in the home.php to show my ‘portfolio’ custom post type, BUT, other regular posts show too?

    how can i only show the custom post types? or is this not possible with the home.php in genesis, might need to just do a page template, and set that as the home page.

    thanks in advance

    • Bill Erickson says

      home.php should really only be used for listing your blog posts. If you want portfolio items on your homepage, I recommend:
      1. Creating a page called “Home”, then go to Settings > Reading and set it as the front page.
      2. If you still want a blog, create a page called “Blog” and in Settings > Reading set it as the posts page (home.php will be used for this page)
      3. You could either create a front-page.php template file for the front page, or a page template and then specify that the homepage is using that template. In this file, build your custom query.

      • Jamie says

        Thanks for clearing that up Bill, you just saved me a few hours of my time, as i was completely on the wrong track. I get it all now.

        Really appreciate your support :)

Leave a Reply

If you'd like to include code in your post, please post it to http://gist.github.com and include a link.