WordPress Custom Post Types

Posted: September 9, 2012 in Development, Wordpress

One of the most versatile features of WordPress is Custom Post Types. There are great benefits of using more than the five native ContentTypes (Post, Pages, images, attachments and so on ), when using WordPress as a CMS. Custom Post Types enable us to create new types of Post or Page like contents, this kind of types are treated as equal citizens to Posts and Pages having their own dedicated Menu Item.

In this Post I will use the canonical example of Event to show you step by step how you can use this great WordPress Feature. For the development examples we will use the Default WordPress Theme, since all we want is to present the technic.

If we remember the basics of the development methodology, typical we use Posts generally for content that are updated frequently and pages are generally used for static content or list’s and container’s of other contents. After the 3.0 version of  WordPress we can now use the concept of Custom

Post Types that allow us to have more specific Types.

Now that you already know the main advantages of using Custom Post’s let’s start by creating a Custom Post to Model an Event.

First let’s open the file functions.php of our theme and start coding our new type.

add_action( 'init', 'create_events' );

this function will add create_events as a function to be called by WordPress
when WordPress Initiates (because we are passing init as the event to hook).

The create_events function will tell to WordPress how to display messages about our custom post type (using the Label attribute), in the
supports array we define what our new type will support. finally with the register_post_type we make the register of the new Type.

function create_events() {
  $labels = array(
    'name' => _x('Events', 'post type general name'),
    'singular_name' => _x('Event', 'post type singular name'),
    'add_new' => _x('Add New', 'Event'),
    'add_new_item' => __('Add New Event'),
    'edit_item' => __('Edit Event'),
    'new_item' => __('New Event'),
    'view_item' => __('View Event'),
    'search_items' => __('Search Events'),
    'not_found' =>  __('No Events found'),
    'not_found_in_trash' => __('No Events found in Trash'),
    'parent_item_colon' => ''
  $supports = array('title', 'editor', 'custom-fields', 'revisions');

  register_post_type( 'event',
      'labels' => $labels,
      'public' => true,
      'supports' => $supports

After entering the code and restarting the WordPress DashBoard we have already the options to create new posts of the Type we have

At this time we are able to create new post’s of the type we have created. At this time maybe you are saying “Ok so i am able to
create my own types but can i define their layout in a different manner than the native WordPress post’s?” And the answer is yes.

On WordPress themes a single post is displayed using the single.php theme file, from the WordPress version 3.0 you are allowed
to add a single-[your_post_type_name].php file to override how a custom post type are presented
. In our case we will not
redefine the way our Custom Post is presented but if we want to do that as you had seen would be very easy.

What would be not so straightforward is how to list our Custom Post’s, is to display a list of custom post types thought the use
of a Custom Page
Template. This is done by copying the contents of page.php to a new file called page-[your_post_type_naem].php.
In the loop of the Page Template you should put the following code

<?php query_posts(array('post_type'=>'event')); ?>

This way you will get all the Event Post’s you have at your Site.

To enable the content manager to choose this Page you should put the following code as an identifier of your page template ate
the top of the file

 * Template Name: Events Template
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0

get_header(); ?>

To use the page you have built you should log back in to the WordPress Dashboard, create a new page with the title of Events,
and then select the “Events Template” for the Template page attribute.

And that’s all, i hope this post has a help for you to learn one-off the most interesting extension features of WordPress.

Foot Note: This Feature is only available in version 3.0 or superior


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s