Blog

Creating custom post types and pulling them into a custom page template in WordPress

Hello!

Occasionally in WordPress it is necessary to push, what is primarily a blogging content management system, to its limits in order to create a dynamic website.

The reason why it may be necessary to create a custom wordpress post type is in just that scenario where you need to create custom and dynamic types of content that go beyond what a wordpress blog “post” by default will allow you to modify.

For example you may want to create a car website that lists all sorts of different cars and has a multiple car listing page and single car listing pages. Each car could have many fields like color, make, model, year, engine, features and so on. Doing this in a wordpress blog post just wouldn’t work because out of the box you only have fields like post title, categories, tags and post content.

While it is possible to simply paste all the data in a table in the WYSIWYG editor, wouldn’t it be nice to have the “color” and “model” fields of that car as separately defined fields? The advantage with that is that you can then “pull” those individual fields and create a really dynamic and amazing page template that arranges all these fields in different ways.

Create a custom post type with a plugin

There are many free and commercial wordpress plugins that accommodate ways to create a custom post types, one of which that is worth noting is the Types plugin. This plugin allows you to circumvent the (minor) php development that would be necessary to manually create your custom post type. You still will need to develop the custom page template that “pulls” this data, but at least the first part of the problem can be done by a web interface instead.

The documentation for this plugin includes an informative how-to video that demonstrates how to create the custom post type, fields and everything in between.

Edit your functions.php to create a custom post type

This method is obviously a little bit more advanced. To anyone who is comfortable with PHP and is familiar with WordPress, this should be quite easy. Basically this method is what the aforementioned plugin is doing through the web interface. An advantage of doing things this way is that you have more options for customizations and perhaps for connecting two different custom post types together such as making fields related or connected.

add_action( 'init', 'custom_post_type' );
function custom_post_type() {
register_post_type( 'the_car',
array(
'labels' => array(
'name' => __( 'Cars' ),
'singular_name' => __( 'Car' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'cars'),
)
);
}

What were doing in the above code snippet is essentially hooking into the init function and adding a new function that creates this new custom post type called “Cars”. The rest of the definitions like “slug” are pretty straightforward.

The next thing we want to do is add custom fields so that when you are adding or editing the custom post types you can add those “year” , “model” and whatever custom fields you feel is necessary :

add_action("admin_init", "admin_init");
 
function admin_init(){
  add_meta_box("car-year_meta", "Car Year", "car_year", "cars", "side", "low");
}

function car_year(){
  global $post;
  $custom = get_post_custom($post->;ID);
  $the_car_year = $custom["car_year"][0];
  ?>
  
  ;
  

That’s it! If this is something you can get comfortable with then you are really opening up the possibilities with WordPress because you can customize the way it handles and displays content to whatever you want, with whatever design considerations that you want as well.