Post Image

How to create Custom Post Types in Wordpress

So, a custom post, what is it? Why do we talk about this for you and how to use them.

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 you 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.

WordPress 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.

Create a Custom Post Type

  1. Open functions.php file and paste the following code.

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' );
  1. Save changes

If you take a look at your admin area in WordPress, you will notice there is a new item “Game Reviews” available on 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:
<?php
/**
 * Template Name: Game Reviews
 **/
?>
  1. Open page.php file which is located in your theme’s folder
  2. Copy and paste the code into you game-review-template.php
  3. Find a piece of code which is the same, or similar to this (might depend on your theme):
<?php   endif; ?>
<?php endwhile; ?>

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:

<?php
 $query = new WP_Query( array('post_type' => 'game-reviews','posts_per_page' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>
   // Your code e.g. "the_content();"
  1. Save changes
  2. Go to Pages -> Add new
  3. Create a page with a name "Game Reviews"
  4. On the right side, under "Page Attributes" tab you should find "Template"

Choose a template you have created in one of the previous steps and assign it to the page.

  1. Save changes

That's it. You should try everything and create a 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 your 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.

In Conclusion

Custom Post Types are one of the greatest WordPress features even today. By simply modifying a few lines of code you can generate a post type that will be unique to your site. The truth is that it might be a little bit harder for a beginner to complete everything for the first time, but if you stick with this tutorial, you should not have any problems. We hope that this article will be useful for you. Good luck!