Getting ready
So, what are custom post types? That’s simple, custom post types are like a blog post or page, but of a custom defined type.
As an example, I decided to list some promo codes on my other blog CatsWhoBlog. I could have used a good old static page, but updating it and adding new promo codes would have been a pain.
So I created a custom post type, named coupon and a page template to list all coupons. It’s as simple as that, and now managing coupons & promo codes is extremely easy:
Creating the post type
Ok, let’s code. The first thing to do is to create a custom post type. To do so, pick up your theme functions.php file, and add the following:
function create_my_post_types() {
register_post_type('coupons',
array(
'label' => __('Coupons'),
'singular_label' => __('Coupon'),
'public' => true,
'supports' => array(
'title',
'excerpt',
'comments',
'custom-fields'
),
'rewrite' => array(
'slug' => 'coupons',
'with_front' => false
),
)
);
}
add_action( 'init', 'create_my_post_types' );Once you saved functions.php, you should notice that a new tab appeared in your WordPress dashboard, as shown in the picture below:

So what does this code do?
First, I have created a function which registers a new post type, named coupons. I gave the following parameters to the register_post_type() function:
- label: Nicename of your post type.
- singular_label: Pretty self explanatory, the singular label of your post type.
- public: Allows post type to be seen publicly.
- supports: Array of data of what the post type supports (editor, excerpt, comments, custom fields, etc…)
- rewrite: Parameters for url rewriting and general post type display.
The complete parameter list can be found on WordPress Codex.
Then, I “hooked” this function to WordPress init() function using add_action().
Adding data
Now that the post type has been created, you can add data by clicking on the “Add Coupon” (Or whatever you named it) link in WordPress dashboard menu.
You should see the following:

Creating a page template to list custom post types
Now that we have created a custom post type and added some custom posts, we still have to display it. To do so, I have used a page template. You can easily reuse the following code, or adapt it to display in your blog sidebar, for example.
If you want to see a demo of the page template, just click here.
<?php
/*
Template Name: Promo codes Page
*/
?>
<?php get_header() ?>
<div id="container">
<div id="content" class="coupons">
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php global $wp_query;
$page_num = $paged;
if($pagenum='') $pagenum=1;
$wp_query = new WP_Query("showposts=20&post_type=coupons&post_status=publish&paged=".$page_num);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="exerpt"><?php the_excerpt(); ?></div>
</div><!-- .post -->
<?php endwhile; ?>
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar() ?>
<?php get_footer() ?>As you can see, the code I’ve used is definitely easy and self-explanatory. In order to fetch a specific post type, you have to specify the parameter post_type=coupons.
That’s all for today, hope you enjoyed this tutorial!







My name is Jean-Baptiste Jung and I'm the man behind Cats Who Code. I started to use the Internet back in 1998 and started to create websites three years laters in 2001.
45 Comments
Thanks, this is really useful! I cant wait to adapt this into upcoming clients websites.
This would make it a lot easier for them to update their own website/blog!
Awesome!
I’ll be useful for my future projects for sure.
Thx a lot!
I think that’s the most interesting new feature of wordpress. It makes wordpress really more flexible and really much closer to a full cms engine than just a simple blog managing platform.
Thanks for this really article Jean-Baptiste! I was just wondering how this new feature should be used.
“Thanks for this really article Jean-Baptiste!”
Lol! I meant:
“Thanks for this really INTERESTING article Jean-Baptiste!”
Great article, really useful feature, I will be using this on some of our client sites no doubt.Thanks for sharing Jean-Baptiste!
Just out of curiosity, how do you enable the “stolen post” feature that you have (I presume accidentally) at the top of this post?
I came here from your feed. The first image from this post that said, I didn’t write this post, i stole it from catswhocode.com.
The rest of the images displayed fine.
Just letting you know
Thanks, bug should be solved now.
The main thing is using custom fields, right?
But I don’t see how to retrieve the fields from the page template
$post_meta = get_post_custom();
It goes inside the while loop of course.
Call print_r($post_meta) after that line so you can understand the array a bit better.
Amazing. I was just looking in to this the other day but didn’t find much but this solves everything. Now to implement it on 6 sites….
Frankly, the work of Creative Commons
Thanks!
Nice tutorial, I think I’m finally able to wrap my head around custom post types.
I become more aware now about custom post type feature.
Thanks….
Hi, im a beginner in wordpress, i was just wondering about “the_content();” that you used there, becuz from what i’ve read in the wordpress codex, it must be inside the Loop?
what´s about paging…
it doesn´t work ;(
i found the solution
replace $page_num with $pagenum
I think I’m doing something wrong, since it’s not working for me…
I did the following:
1) copied the create_my_post_types() function and saved it as a plugin (and activated).
2) created a new page template ‘coupons.php’, c&p-ed the loop and saved it at my theme.
3) made a new page called ‘Coupons’ (slug ‘/coupons’), without content but with page template.
4) created a new coupon with the new post type and published it.
When I go directly to the coupon (eg /coupons/lorem-ipsum) it works, but it won’t be shown in the loop at /coupons
I’ve been looking into this since RC2 but I’ve been trying to use taxonomies instead of custom fields. Should I be using custom fields or is it possible (better?) to use taxonomies? Can you show an example, especially the coding of the template. I’m just starting with WP and I copied the page.php to template-store.php and was trying to tweak that instead. Also, I was using the “Custom Post Type UI” instead of altering my functions.php and wanted your opinion on that plugin. TIA!
hey thanks for writing up this tutorial!
How is the custom post type different than just creating a custom template for posts in the category ‘coupons’ and using code on the single.php template like shown below to call in the custom template? I hope this doesn’t seem like a stupid question. I just want to get a better understanding of what the custom post type accomplishes. Maybe it does the same thing, but makes it easier to do without hacking a template. Thanks for any reply and here is the code I have used before to accomplish the same thing (I think):
Really useful tutorial. The custom post is a way to make it easier to use wordpress
Good post, although I fail to see how this new feature in 3.0 is better than what the “magic fields” plugin already does. Not only has it been around pre 3.0 but its alot easier to use.
No need for all that code – making new post types and managing them is done through the admin panel.
Thanks for the post! It really is a very neat feature of WP3.
What you don’t quite mention is the convention to name the template file and where to put it. I guess you named it “coupons.php” and put it in the theme root folder?
Santé
Hey, thanks for getting us all rolling on 3.0!
Is there a file naming convention like category-coupons.php (for custom category templates) or page-coupons.php for these custom post type template files?
I am assuming since this is not “technically” a post OR a page that we need to create a custom “page” template to be treated as a archive-like listing.
IMO I wish they wouldn’t have called it “custom POST types”. That makes you think you would need to create a category for it. Could have been “Custom content types” or something.
I have just upgrade my wordpress into 3.0 so this tutorial would be new experience for me, i will try this one.
Thanks for sharing the entire creating side blog process. I’ve always been confused/had trouble with the codes because sometimes they just get really complicated and confusing so the one you used is pretty straight forward and easily applicable. Wordpress 3.0 is way better. I love the upgrades!
Wow… great post now this task become very easy & after reading your post carefully any one can design & develop side blog with wordpress 3.0. I like your way for explaining all the things like your step by step snap shots etc.
This is a really nice pracitcal example. I’ve been deliberating as to whether I should set up a sepearate sub-blog or run a seperate RSS feed for a specific category which doesn’t fit with the main blog content. But I think I’m going to use the method you’ve shown. Thanks
WP 3.0 is nice!
I am still using the following in my Functions file to create post templates:
‘term_id}.php”) ) return TEMPLATEPATH . “/single-{$cat->term_id}.php”; } return $t;’ )); ?>’
Maybe its time I switch over, should be fun and your post will help me get it done much quicker, thanks!
Wow the custom post type feature rocks … Wordpress keeps on raising the bar for CMS-Systems. I would definitely prefer Wordpress over typo3 for customers with basic web-knowledge.
Im def switinching over these new examples are really helpful. WP3 could be better but i guess ill have to settle for this for the time being
Another great post. I have yet to delve into the new features of WP 3.0 but what I have been finding, I’m liking. Thank you for detailing this new feature. I’m looking forward to using it. Thanks agian.
Thanks a lot !! I was wondering whether this was possible or not till I found your post.I am going to adopt this on one of my websites.Thanks again Jean.
I had a lot of trouble with this one but I think I’ve sorted it out now..
1) The template needs to be saved as single-yourcustomposttypename.php and it will be used as the default template for this post type.
2) Refresh your permalinks structure and enable ‘rewrite’ => true, in the register post type settings if you are getting 404 errors.
It would also be nice to have Customised update messages when you have edited a page so use the following snippet for this.
Then to get custom messages you need to add this filter to your functions.php file
Replace yourcustomposttype and Custom with appropriate values for your theme.
add_filter(’post_updated_messages’, ‘yourcustomposttype_updated_messages’);
function yourcustomposttype_updated_messages( $messages ) {
$messages['yourcustomposttype'] = array(
0 => ”, // Unused. Messages start at index 1.
1 => sprintf( __(’Custom updated. View Custom‘), esc_url( get_permalink($post_ID) ) ),
2 => __(’Custom field updated.’),
3 => __(’Custom field deleted.’),
4 => __(’Custom updated.’),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __(’Custom restored to revision from %s’), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __(’Custom published. View Custom‘), esc_url( get_permalink($post_ID) ) ),
7 => __(’Custom saved.’),
8 => sprintf( __(’Custom submitted. Preview Custom‘), esc_url( add_query_arg( ‘preview’, ‘true’, get_permalink($post_ID) ) ) ),
9 => sprintf( __(’Custom scheduled for: %1$s. Preview Custom‘),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( ‘M j, Y @ G:i’ ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __(’Custom draft updated. Preview Custom‘), esc_url( add_query_arg( ‘preview’, ‘true’, get_permalink($post_ID) ) ) ),
);
return $messages;
}
Thanks for sharing this, after seeing what you have done with this I have some really exciting ideas for my future skin releases. I’ve already rolled out limited solutions on some clients sites but my guess is we are going to see some really interesting things being built with Wordpress in the coming months!
I love version 3.0 of Wordpress, I feel it’s almost too good to be true that’s it’s still free. Your post ads more possibilities which I appreciate greatly.
Many thanks.
Thanks for the great tutorial!
but i think i’ve found one problem.. the search is including these coupons on the search.
Exemple: http://www.catswhoblog.com/?s=WP%20WebHost
How can i exclude this custom post from the results page?
Thanks again and keep up the good work!
example*
This is super awesome. I am imagining all kinds of possibilities. You’ve made my inner nerd really happen, Jean-Baptiste! Thanks so much for posting this. (:
Jean, good post.
I like the idea of the side blog as I was searching the forums looking for this very thing.
My question is how do I use that space to insert a banner ad?
Also I followed your instructions and was able to add it to wordpress but not real sure how to apply it. If your saying this guy must be a newbie your right.And I would love your help. Thanks.
Wow, I’m usually totally lost when it comes to tutorials that use programming, but this one was very clear and extremely useful. Maybe a follow-up post could be something like “10 custom templates to add to your blog”
Nice tutorial. I recently started a blog on WP 3.0 and I’ve been very happy with the features!
Great article, I didn’t know it would be that simple. Thanks!
This is a neat feature and pretty simple to customize. Thanks for posting!
11 Trackbacks
=== popurls.com === popular today…
yeah! this story has entered the popular today section on popurls.com…
[...] here to see the original: How to create a side blog with WordPress 3.0 21 June 2010 | Uncategorized | Trackback | del.icio.us | Stumble it! | View Count : 0 Next Post [...]
[...] Link: http://www.catswhocode.com/blog/…; [...]
[...] How To Create A Side Blog With WordPress 3.0 [...]
[...] the way, did you had a look to my latest post on CatsWhoCode about WP 3.0 custom post types? If you enjoyed this article, please consider sharing it! [...]
[...] Metaboxes, Querying for Custom Post types The Essential Guide to WordPress 3.0 Custom Taxonomies How To Create A Side Blog With WordPress 3.0 How to Enable Wordpress MU into Wordpress 3.0 WordPress 3.0: Multisite Domain Mapping Tutorial [...]
[...] How to create a side blog with WordPress 3.0. In this tutorial, I’ll show you how to create a side blog listing products using the [...]
[...] There is a lot you can do with the new feature custom post types and over at Cats Who Code Jean-Baptiste has a great idea ‘How to create a side blog with WordPress 3.0‘ [...]
[...] How to create a side blog with WordPress 3.0 [...]
[...] your new blog by using it as an example. This is what I’ve done on this CWC post named How to create a side blog with WordPress 3.0. The post is an useful tutorial which use my coupons page as an example. Result: 288 Retweets as I [...]
[...] a Side Blog with WordPress 3.0, I will show you how to do it. This tutorial is original created by CatsWhoCode, Thanks for share this wonderful [...]