Creating Custom Post Types in WordPress (using a plugin)
Post Types refers various structured data. WordPress come up with default Post Types. Such as post, page, attachment, revision and nav-menu-item. What happens if you want to create your own post type. That could be possible and obviously WordPress support for Custom Post Types. Lets see how we can use this feature in WordPress.
Before starting the coding you need to know there were reserved Post Types and we can’t use those because WordPress is using.
Addition to these following Post Types should not be used.
Other than these any name you can use for creating Custom Post Types. If you can prefix the Custom Post name, that is a good practice.
Definition for registering a post type is
<?php register_post_type( $post_type, $args ); ?>
$post_type – Name of the Post_type. Name should be a
String and it is
Required. Also can contain maximum of 20 characters without any capital letters or spaces.
$args – This is optional and contains an array of arguments.
Lets see an example for registering a Post Type. I am creating a post type for Book.
Creating a Custom Post Type can implement in two ways. Using a Plugin or code it manually. If you are new to this recommended way is using a Plugin. Otherwise manual way is fine. No matter.
How to create Custom Post Type using a Plugin
The best plugin for Creating Custom Post Types is Custom Post Type UI by WebDevStudios. Install this plugin by navigating to
Plugins > Add New. Activate the Plugin. This will add
CPT UI menu item to your Admin Menu. Whether this plugin supports for Custom Taxonomies we are only discussing about Custom Post Types in this tutorial. Lets move forward. Once you are ready with the Activated Plugin navigate to
CPT UI > Add/Edit Post Types.
Add New Post Type tab contains the settings for creating a post type. It contains Basic Settings , Additional Labels and Settings .
Basic Settings are mandatory fields. Here you need to define the Post Slug, Plural Label and Singular Label. You need to fill these settings to create the post type.
You can change menu names, item labels, etc under this.
Under settings you can change many things as you want. Even you can define capability here. Other than these you can define Post Type supports under this Settings tab. Whether this post type supports for Featured Images, Editor, Revisions, Author, etc… There were many and you can see those by navigating to CPT UI > Add/Edit Post Types .
Lets fill the Post Type. As we are creating a Post Type for Books, lets name the fields as we want and click the Add Post Type . Please refer the image. You’re done with creating and Book Post Type has been added to your admin menu.
Now you can add Book Posts by Navigating to Books > Add New . Fill the Title, Editor and Featured Image. By Publish the post, you can add Books Posts.
How to show Post Types in Front End
You can query to get posts. Assume you want to show these Post Types in Books page, you need to query for Post Type in Books page. Here is the code
<?php $args = array( 'numberposts' => 5, 'post_type' => 'book', 'orderby' => 'post_date', 'order' => 'DESC' ); $loop = new WP_Query( $args ); while($loop->have_posts()) : $loop->the_post(); ?> <div class="book-item"> <?php the_post_thumbnail(); ?> <h3 class="book-content"> <h3><?php the_title(); ?></h3> <?php the_content(); ?> </h3> </div> <?php endwhile; ?>
This will displays 5 Posts in Books Page. You can also define Permalink (
) for this each item. Detailed page of Post Type is
single.php. If you need to create a separate Single Page for this Post Type you need to create
single–book.php. This will create separate single page for book post type. You can call the contents where you want in this page.
We have done with the Create a Custom Post Type with a Plugin. If you want to learn How to create a Custom Post Type without a Plugin in WordPress ? Lets navigate.