Most of wordpress users are familiar with wordpress custom post types and the flexibility it has brought to wordpress.
WordPress was first developed as a blogging platform and even now it is meant to blog when you have the default installation of wordpress, but with the release of wordpress 3.0 it became a vast CMS which can power up any type of site one can imagine.
Do you know what was so magical in wordpress 3.0?, if you guessed the addition of custom post types capability then you are absolutely right.
This tutorial is basically to answer all your questions about WordPress “custom post types” including what they are, how they are created and how they are displayed on the front end.
What are WordPress custom post types?
Basically “post_type” is a database field in “wp_posts” wp database table which comes in with the default installation of wordpress. By default wordpress give us several post type options to choose from which includes post, attachment, page, revision and navigation etc; but does not limit us to use only these.
WordPress Custom Post Type Uses:
WordPress custom post types comes in handy if you want to create a company portfolio website, an ecommerce website, a product catalog, a movie review site, books library, or a product review site.
The power of custom post types led wordpress to be recognized as the most used content management system over the internet. You can create as many post types as you like, there is no limit on creating custom post types, all you should know is how to use custom post types and when to use these.
How to use custom post type in wordpress:
You can use the existing plugins for creating post types or can create your own plugin if you know a little bit of coding and are familiar with wordpress. I have shared the code below for creating a custom post type which can be used in a new plugin or in functions.php file of your current wordpress theme.
Adding custom post type to wordpress:
This is the code you need for creating a new post type, in this code I have created a custom post type named “ reviews”, i have used this code in “functions.php” file but it can also be used in any plugin if you want to, functions and argument listed in this code are explained below.
function create_postType_review() {
register_post_type( 'reviews',
array(
'labels' => array(
'name' => 'Reviews',
'singular_name' => 'Review',
'add_new' => 'Add New Review',
'add_new_item' => 'Add New Review',
'edit' => 'Edit',
'edit_item' => 'Edit Review',
'new_item' => 'New Review',
'view' => 'View',
'view_item' => 'View Review',
'search_items' => 'Search Reviews',
'not_found' => 'No Reviews found',
'not_found_in_trash' => 'No Reviews found in Trash',
'parent' => 'Parent Review'
),
'supports'=> array(
'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array('review_categories','post_tag'), // if you don't have custom taxonomy comment out this line.
'public' => true,
'menu_position' => 15,
//'menu_icon' => plugins_url( 'images/image.png', __FILE__ ), if you have created an icon for your custom post type, replace image.png.
'has_archive' => true
)
);
}
add_action( 'init', 'create_postType_review' );
WordPress Register Post Type:
register_post_type ($post_type, $args); This function is responsible for creating custom post type in wordpress, it requires two arguments “post_type” and “array of labels”.
It also supports an optional array “supports” in which different items can be listed which will be displayed at the admin section of custom post type. post_type : this is the name for the post type we will create, in this case it will be reviews. args: these arguments may be a single array or array of multiple arrays, most of these arguments are optional but i have explained which i have used in the above code.
labels (array) (optional) labels – An array of labels for this post type. By default, post labels are used for non-hierarchical post types and page labels for hierarchical ones.
Default: if empty, ‘name’ is set to value of ‘label’, and ‘singular_name’ is set to value of ‘name’. if you want to learn more about labels click here.
supports (optional array): This is where we have to list all the items which we want to access at the admin section when creating a new product review. If supports array is not specified or passed to the register_post_type() function the default fields will be displayed, which are title and editor.
In our case for product-reviews we will have access to title, editor, author, excerpt, thumbnail, comments revision and custom fields. public (optional): if you want this post type to be excluded from search and hide from other authors then make it “false”. ‘public’ => true, means that it this post type will be shown in admin ui, will be publicly query-able and will be shown in navigation menu at admin panel.
has_archive (optional Boolean/string) : Its default value is “false”, setting it to true will enable post type archives. Now that you have understood the code its time to place it in your theme’s functions.php file and making a template for reviews post type.
Creating WordPress Custom Post type templates:
For a newly created custom post type two theme templates (single-{post_type}.php and archive-{post_type}.php) should be created for its proper functioning.
single-{post_type}.php:
Better way to create this template is to copy and paste “single.php” and rename it to match the required post type. For reviews we will create single-reviews.php
archives-reviews.php:
Copy and paste archive.php and rename it to archive-reviews.php.
Final Step: Every thing is ready and every bit required for wordpress custom post type is in place, it is time to flush rewrite rules. From wordpress admin go to settings > permalinks and click save changes button without changing any thing. This will enable wordpress to recognize permalink structure of newly created post type.
Note: In case of any error feel free to ask in comments, Do share with us if you have any better solution.
Nice tutorial for wordpress custom post types! This is massively helpful to me. It just made my work easier. I really like this tutorial. It saved lots of time.
Thanks.