WordPress Custom Post Types: What Is It and How It Can Be Created?

Filed Under: WordPress

WordPress has come a long way – from being known as just a great blogging platform – and is today touted as the most popular content management system. WordPress took years to establish itself as a strong contender in the CMS industry, but what actually proved a game changer was the introduction of “Custom Post Types”. This mechanism was rolled out with WordPress version 3.0, and made it possible for users to create a wide variety of content.

This post will provide useful insights on Custom Post Type and how you can create one tailored to your specific needs.

What is Custom Post Type?

Custom post types are just like your regular posts except that they have different post_type values. Although, WordPress has evolved from being used as a blogging platform into a full-fledged CMS, but some people mistakenly classify “Post Type” as simply a post. But, it can be any kind of content.

WordPress comes with some default post types like Post, Page, Revision, etc. However, you can even create your own custom post types. For example, if you’re running a Photography WordPress website, then you can create a ‘Photography’ post type. You can even create custom post types for Testimonials, Reviews, etc.

Understanding the Purpose Behind Creating Custom Post Types

Let’s assume, you would like to segregate your content on your site in a structured manner, for instance, you might want to define different categories within your blog, or wish to add posts by multiple authors and lot more. But, what if you want more that cannot be achieved with help of default post types? You can add possibly anything with help of a custom post type.

How You Can Create a Custom Post Type?

There are two different ways following which you can create a custom post type. Let us take a look at both these ways:

  1. Creating Custom Post Types Via Plugin

    One of the easiest trick or way that will help in creating a custom post type requires you to use a plugin. Especially, novices or non-programmers find this an ideal way to accomplish their goals.

    Custom Post Type UI is an excellent plugin that helps to create as well as manage custom posts types in your WordPress admin menu. It is compatibile with WordPress version 3.5 or higher. You can search for this plugin from the official WordPress Plugin Repository. And then, activate and install this plugin by following the below mentioned steps:

    • Open your WordPress dashboard, go to Plugins > Add New. And then, Search for Custom Post Type UI.
    • Click on the “Activate Plugin” link as shown in the screenshot below.

      Custom-Post-Type-UI_instll2

    • The Custom Post Type UI will get added to your admin menu.
  2. Creating Custom Post Type Manually

    Though WordPress plugins provide a hassle-free way to create custom post types, but there is a downside to using a plugin: your custom post types will no longer be visible once the plugin is deactivated. Although, the data of those custom post types will still exist, but you won’t be able to access your custom post type as it will become unregistered. In that case manually creating a custom post type will prove a viable alternative.

    For creating a custom post type, we’ll need to register our custom post type using the register_post_type() function. For example, let us create a code that register “Movies” as our custom post type. This code should be added to the function.php file:

    
    // Our custom post type function
    function create_posttype() {
    register_post_type( 'movies',
    // CPT Options
    array(
    'labels' => array(
    'name' => __( 'Movies' ),
    'singular_name' => __( 'Movie' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'movies'),
    );
    }
    
    // Hooking up our function to theme setup
    add_action( 'init', 'create_posttype' );
    

    The above code will create a post type “Movies”. Next, we’ve defined an with an array with a few arguments that serves as the options of our custom post type. As you can see, the array is divided into two different parts. The first part contains some labels which define the custom post type name in two forms: singular and plural. The second part consists of other arguments, which includes public visibility, has_archive, and slug that will be used in your post type URLs.

    You can choose to add even more options to your custom post type, in order to enhance its capabilities.

    How to Display Custom Post Types on Your Site?

    As we all know, WordPress provides built-in support for different features. Likewise, it even provide support to display your custom post types. There are a lot of ways using which you can display your post types. In our case, we will talk about how you can showcase them on your front page. For this, add the following code in your theme’s functions.php file:

    
    add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
    
    function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'movies' ) );
    return $query;
    }
    

Wrapping Up!

Do you wish to improve your post type capabilities or simply want to add some new ones? You can create custom post types to meet such objectives. There is a lot more you can do with custom post types. Hopefully, this post will help you understand how you can create your own custom post type in the most effective and efficient manner.

About the Author: Edward Jones is a developer who keeps sharing his experience through his insightful blogs. If you need to hire professional wordpress programmer then simply get in touch with Edward via Twitter and Google+.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages