How to create Custom Post Types in WordPress

Before we actually show you how to create your own custom post type which you’ll be proudly using on your new WordPress powered website, let’s see what a custom post type actually is.
A post type is simply a set of rules which are used to describe the way content is presented in your article. Each post type has its own parameters which define it so you can tell a difference between a post, page, archive, revision or a navigation menu. But as you will see, if your start writing articles as your posts, everything you write will have the same structure. That’s because you will use the same template for the post. But that is completely OK if you write a blog where one post type is enough to handle your articles.

Through the not so long history, WordPress has evolved tremendously and it has definitely outgrown its role as a blog platform. It has become one of the most used and appreciated content management systems in the world. Allowing people to create custom post types is one of the main reasons for that.

Let’s see what a custom post type can do for you in a simple example. Imagine you are running gaming website. You want to publish news on a regular basis, there are numerous trailers published every day which you can share with your audience, your authors play new games and write reviews, you want to publish screenshots from your latest online match, etc. As you can already guess, there are several content types you would need in this case and if you continue on using regular posts, all of the content will look the same and it will make it harder for you and for your visitors to find a difference between a video article and a review. Yes, you want a custom post type to make everything look more organized.

Stay with that picture just for a few more seconds. If you create a custom post type, your news could have a special area where you can place a link to the source. Your game review post type could have a rating the author gave to a certain game including a star-based system, and you can apply different styling for, let’s say, the subtitle of your review. Your trailers post type would accentuate a video and let people enjoy it from a lightbox instead of viewing it in a post. We can go on forever, but you get the point, right?

Create a Custom Post Type:

OK, we know you are getting bored with our talk – you are here to learn how to create a custom post so let’s start. Stretch your muscles, take a deep breath and when you are ready, let’s start:

  1. Open functions.php file and paste the following code.
  2. Make sure the code is placed before the closing PHP tag (?>) or it won’t work.

    // Creates Game Reviews Custom Post Type
    function game_reviews_init() {
        $args = array(
          'label' => 'Game Reviews',
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'rewrite' => array('slug' => 'game-reviews'),
            'query_var' => true,
            'menu_icon' => 'dashicons-video-alt',
            'supports' => array(
                'title',
                'editor',
                'excerpt',
                'trackbacks',
                'custom-fields',
                'comments',
                'revisions',
                'thumbnail',
                'author',
                'page-attributes',)
            );
        register_post_type( 'game-reviews', $args );
    }
    add_action( 'init', 'game_reviews_init' );
  3. Save changes

If you look at your admin area in WordPress, you will notice there is a new item “Game Reviews” available in the menu. So far, it will look like any other post but you will still not be able to see these posts online. You have few more steps to follow through in order to make the most out of this custom post type.

Be sure to set up your permalinks before you try your new post or you’ll end up with an error .

Create a template for your new custom post type:

  1. Open your theme’s folder
  2. Create a new file and name it something like game-review-template.php
  3. Open the file and paste the following:
  4. Open page.php file which is located in your theme’s folder
  5. Copy and paste the code into you game-review-template.php
  6. Find a piece of code which is the same, or similar to this (might depend on your theme):
  7. When you have located this part, let’s modify it so you can show your custom post type on a new page. You should add the $query line above your loop and then modify the loop itself. When you modify the code, it should look like this:

     'game-reviews','posts_per_page' => 5 ) );
     while ( $query->have_posts() ) : $query->the_post(); ?>
    // Your code e.g. "the_content();"
    

    Save changes

  8. Go to Pages -> Add new
  9. Create a page with a name “Game Reviews”
  10. On the right side, under “Page Attributes” tab you should find “Template”
  11. Choose a template you have created in one of the previous steps and assign it to the page.

  12. Save changes

That’s it. You should try everything and create new Game Reviews post. Once you open your Game Review page, you should be able to see your news post types lined up chronologically just like you normal post would be.

And this is just the beginning. You have created the custom post type, but now you should modify it the way you like it and add the code which you need.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *