Creating Custom Post-types in WordPress

If you are thinking of converting your WordPress blog into a CMS(Content Management System), then there is nothing wrong with it. In fact WordPress has been widely used as a CMS. This is due to the fact that WordPress is so much flexible that we can do nearly any type of modification in its functionality. This time we will create custom post-types in WordPress. WordPress already provides ‘posts’ and ‘pages’ as post types, but if they don’t suit your needs, you can create your own post types. So let’s see how it can be done. We will create a new plugin ‘Add Persons’ for enabling custom post-types.

Initializing our plugin:

Plugin Name: Add Persons
Plugin URI:
Description: Add new Persons.
Author: Arvind Bhardwaj
Version: 1.0
Author URI:

We have just created our new plugin with name ‘Add Persons’. Plugin creation has been descibed in the previous post.

Creating New Post-Type:
WordPress provides a very powerful function for creating custom post-types that will take all headaches for us and make us free from writing any clumsy code. The function is ‘register_post_type
The usage goes as:

<?php register_post_type( $post_type, $args ) ?> 

The function ‘register_post_type‘ takes two parameters $post_type and $args. Post tpye is the type of post that we are creating. In this example we will create post of type ‘person’.
$args is an array of arguments. Complete reference of the arguments can be found at Function Reference/register post type

Lets write the code for it:

add_action( 'init', 'create_Person' );/*Called when WrdPress initializes*/

function create_Person() {
register_post_type( 'person',
'labels' => array(
'name' => __( 'Persons' ), /*general name for the post type, usually plural*/
'singular_name' => __( 'Persons' ), /*name for one object of this post type*/
'add_new' => __( 'Add Person' ), /*the add new text*/
'add_new_item' => __( 'Add New Person' ), /*the add new item text*/
'edit' => __( 'Edit' ), /*the edit text*/
'edit_item' => __( 'Edit Person' ), /*the edit text*/
'new_item' => __( 'New Person' ), /*the new item text*/
'view' => __( 'View Person' ), /*the view text*/
'view_item' => __( 'View Person' ), /*the view item text*/
'search_items' => __( 'Search Persons' ), /*the search items text*/
'not_found' => __( 'No Persons found' ), /*the not found text*/
'not_found_in_trash' => __( 'No Persons found in Trash' ), /*the not found in trash text*/
'parent' => __( 'Parent Person' ), /*the parent text*/
'description' => 'Add new Person to your site!' /*A short descriptive summary of what the post type is*/
'public' => true, /*Meta argument used to define default values for publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search*/
'show_ui' => true, /*Whether to generate a default UI for managing this post type*/
'publicly_queryable' => true, /*Whether post_type queries can be performed from the front end*/
'exclude_from_search' => false, /*Whether to exclude posts with this post type from search results*/
'menu_position' => 5, /*below posts*/
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ), /*the meta boxes to be shown on the add post page*/
'taxonomies' => array('post_tag','category'), /*taxonomies to be used*/

The parameters are self descriptive in themselves. Comments have been added to clarify the usage.

Accessing our custom post-type:

<?php $loop = new WP_Query( array( 'post_type' => 'person', 'posts_per_page' => 10 ) ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>

<div class="entry-content">

<?php the_content(); ?>


<?php endwhile; ?>

Passing a ‘post_type’ parameter in WP_Query can make your life simpler. Now you can access you new post-types just like ordinary posts.
So thats all for our custom post-types. We will meet again for custom meta boxes!

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.


9 thoughts on “Creating Custom Post-types in WordPress

  1. Thanks for adding the comments to the array of labels – that’s the sort of thing that seems obvious but can be confusing. Cleared up a couple of things for me!

  2. I delight in, lead to I found just what I was
    having a look for. You have ended my four day lengthy hunt!

    God Bless you man. Have a nice day. Bye

  3. Definitely believe that that you stated. Your favorite reason seemed to be on the net the simplest thing to take into account of.
    I say to you, I definitely get irked at the same
    time as folks think about concerns that they plainly do not recognise about.
    You controlled to hit the nail upon the highest and defined out the whole thing
    without having side effect , folks can take a signal. Will
    likely be again to get more. Thanks

  4. Good web site you have here.. It's difficult to find high-quality writing like yours
    nowadays. I honestly appreciate people like you! Take care!!

  5. Hello, i feel that i noticed you visited
    my blog thus i got here to return the choose?.I am attempting to in finding issues to enhance my
    web site!I assume its good enough to use some of your concepts!!

  6. Magnificent goods from you, man. I've understand your stuff previous
    to and you are just extremely excellent. I
    really like what you've acquired here, certainly like
    what you're saying and the way in which you say it.
    You make it enjoyable and you still take care of to keep it wise.
    I can't wait to read much more from you. This is really a terrific web site.

Comments are closed.