A Complete Guide to WordPress Custom Post Type For WordPress Beginners!

A Complete Guide to WordPress Custom Post Type For WordPress Beginners!

While WordPress is used and known mostly as a blogging platform, what most people miss out is that WordPress is a robust and advanced Content Management System that is used by various companies and individuals all over the world. Gaining a foothold in the game, WordPress now enables the users to create a wide variety of content other than the default posts and pages.

These custom contents that we can create is known as the WordPress Custom Post type. And to give our users an insight into what exactly does it mean and how do we generate these post types, we have indulged ourselves in this thorough article!

What is WordPress Custom Post Type?

A simple definition of the Custom Post Type would be Content types like Posts or Pages that you can customize and add to your preference. They are very much like any normal posts. However, the only difference is that it displays a different value for post_type inside the database. This means that unlike the default values like page, attachment, and post, the MySQL databases display a different value for them.

By default, WordPress provides several post types that are readily available for the users when you install. For the convenience of our users and a clear understanding, we have explained in a line or two about each Post Type.A Complete Guide to WordPress Custom Post Type? ūüôā #wordpress_custom_post_type Click To Tweet

  • Post: This is a Post Type that is mostly used by blogs to submit contents and to create feeds.
  • Pages: This is similar to post but does not follow the time-based listings of posts. This can be organized in a structure with parent pages but they cannot be assigned to categories and tags.
  • Attachments: This is a special kind of post that holds the information about a specific file uploaded with the WordPress Media Upload system. This includes any description, title, metadata information and so on.
  • Revision: This Post Type holds draft posts as well as the revisions made to specific posts. Basically, they are identical to the posts which they belong to but is differentiated with the posts as their parent using post_parent column.
  • Navigation Menu: This holds the information about a single item placed on the WordPress nav menu system.

Why Use WordPress Custom Post Type?

Now that you have an idea about what exactly is a WordPress Custom Post Type, let’s talk about what you can achieve with it? To give you an excellent example for you to understand the importance of CPT, imagine that you own a website that deals with reviewing products. While the reviews can be contained in a single blog post, there might be elements that you wish to change.

For instance, you might wish to display the brand and the location of the product in a more striking and prominent manner. This even enables the users to access and search for the product by these taxonomies. Now let’s talk about advertisements and affiliate links. Imagine that you need to add a Buy Now Button somewhere on the post to send interested visitors to the site. While the default Featured Image section works with most posts, you might come across times when you want to change the default layout structure. Sounds like a ton of customizing and modifying right?

This is where custom post type or CPT comes in handy. With these, you can easily create a post type that you prefer and meets your requirements. It includes custom fields where you can add elements that you need. If you wish to remove any meta information, you have the option for it as well. Modifying is much easier and takes lot less time compared to the other method.

How to Create a Custom Post Type?

Now that we have talked about the importance and advantages of WordPress Custom Post Type, let’s start the process of creating one. For the convenience of our users, we will be enlisting two methods that enable the users to easily create a WordPress CPT’s.

Using a WordPress Plugin to Create CPT

This is an easier alternative to creating a WordPress custom post type for beginners and novice. Rather than going through the whole process yourself, let a plugin do the job for you. To start off the process install the plugin: Custom Post Type UI.

If you have any confusion regarding the installation process, here is a handy and thorough tutorial by Beautiful Themes: How to Install and Activate a WordPress Plugin?

custom-post-type-step-w

Now that you have installed the plugin, go to your Dashboard and select the newly added WordPress Admin menu: CPT UI.  The plugin allows you to create a new custom post type or if you wish, it also enables you to add custom taxonomies. Select Add New, this will redirect you to the CPT settings to create a new one.

custom-post-type-step-2

Basic Settings

Provide the name for the custom post type. This field has a limit that cannot exceed more than 20 characters. The next field is for the label for your WordPress custom post type. It will appear in the admin bar just like any other Posts and Pages. This works similar to a Category option. Add both the singular and plural labels.

Next, enter the description for your custom post type. This is simply used to define and describe what the custom post type is about. You will then find several other options to customize the elements of the custom post type.

custom-post-type-step-3

Additional Settings

You can add Menu Name, All Items, Add New button, Add New Item button, Edit Item and so on that will appear in the Admin menu. There are also settings for Features Images and Archive. Customize each setting to your preference and your requirement.

If you scroll down further, there you will also find the option to edit the post settings. You can choose to display or remove them from the admin UI, Nav bar or REST API. You can also change the queriable feature from front-end, add menu icon, capability type and much more. Save the Changes once you are done.

Now go ahead and find the newly added Custom Post type in the Admin Menu. You can create any new posts just like you normally would displaying the customized settings!


Manually Creating a WordPress Custom Post Type!

Now let’s talk about a more complex but surefire way to achieve results. While the custom post type disappears once you deactivate the plugin, this method makes sure it stays. This is best when you are working on a client site do not wish to install an additional plugin.

But before we get into the process of manually writing code, here is an example so that you have a better understanding.

// Our custom post type function
function create_posttype() {

    register_post_type( 'example',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'examples' ),
                'singular_name' => __( 'example' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'example'),
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

Here, the codes written will register a post type “Example” with an array of arguments. These work as the options to customize the post type. The first part denotes the option for labels. The second half contains the codes for public visibility, has_archive as well as rewrite options and the slug. These are the basic settings to create your custom post type.

Now that you have a better understanding of the codes, here is a detailed piece of codes that provides more options for customizing it further.

</pre>

/*
* Creating a function to create our CPT
*/
function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Example', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Exampe', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Example', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Example', 'twentythirteen' ),
        'all_items'           => __( 'All Example', 'twentythirteen' ),
        'view_item'           => __( 'View Example', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Example', 'twentythirteen' ),
        'add_new'             => __( 'Add Example', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Example', 'twentythirteen' ),
        'update_item'         => __( 'Update Example', 'twentythirteen' ),
        'search_items'        => __( 'Search Example', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'example', 'twentythirteen' ),
        'description'         => __( 'Example news and reviews', 'twentythirteen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy.
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'movies', $args );

}
/* Hook into the 'init' action so that the function
>* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );

With these additional implementations, you can add an array of options for a more effective feel. It enables the users to add more features like support for revisions, add or remove featured images, custom fields and so on. As we have mentioned it now and again, Custom Post Type expand the idea of a post or a page by giving the users full control over the content stored! And with the codes you implement, as long as they remain active on your WordPress functions.php, the CPTs are registered and active on your websites. However, when you remove the snippet of codes, the active stage is lost, but the CPT is still saved on your WordPress database.

How to display Custom Post Type on WordPress?

Now that we have talked about how to create your very own Custom Post type for WordPress, let us now get into the process of displaying them on your site. There is more than one method to do so. So for the convenience of our users, we will be discussing them all thoroughly.

Displaying Custom Post Type Using Default Archive Template

The first method we will be talking about how to display your custom post type for WordPress with the Archive template. To start off, log in with your credentials and go to your Dashboard. Next, click on the option for Appearance and then Menus.

This will redirect you to the Menu settings where you can customize the elements. There you will see the option to add a custom link. There you can enter the link to your custom post type.

If your website uses SEO friendly permalinks, then your URL’s will seemingly look like this: http://example.com/example. And if your website does not use SEO friendly permalinks then your URL’s will likely to look like this:¬†http://example.com/?post_type=example

You can go ahead and paste the URL that suits your website type. Now replace the example.com with your domain name and the example with your custom post type name. Save your Changes and you are done! You will now see a newly added menu for your custom post type archive page!


Displaying Custom Post Types on  the Front Page

While custom post types work great to make your website stand out, you even have the option to display them alongside the regular posts and pages. For this, we have enlisted the code you can add to your WordPress function.php file. Simply copy and paste the coding and you are done!

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;


Displaying Custom Post Type in Widgets

If you are familiar with the WordPress interface then you might know that there is a default widget that allows the users to display recent posts. But what if you want to display your custom posts in the widget instead? Well, the process is simpler than you think thanks to the WordPress plugin Ultimate Posts Widget.

custom-post-type-step-3

Install and Activate the plugin to start off. When activated, go to your Dashboard and select Appearance and then on Widgets. This will redirect you to the widget settings page.

custom-post-type-step-4

There you will find the newly added widget Ultimate Posts Widget. Drag and Drop it to the desired sidebar. This amazing widget allows any users to easily display recent post types from any post type.  Customize the settings to your preference. Once you are done Save the Changes.

Final Thoughts!

With our article today we have talked about custom post type for WordPress. With a thorough thought into the process of creating and displaying the posts, we hope we have answered all of your queries. Custom Post Type is an alternative to creating different and unique post type. With these, you can easily create a post type that you prefer and meets your requirements. You can include custom fields where you can add elements that you need. If you wish to remove any meta information, you have the option for it as well. Modifying is much easier and takes lot less time compared to the other method. Thus with our tutorial today, we hope you can now easily create and display any Custom Post Type for WordPress with ease!

Liked our article? Why not check out some of our other handy and useful WordPress tutorials below?


 

Sam

An avid reader and aspiring writer, I love to research, write and blog about things that peak my interest and curiosity.

5 thoughts on “A Complete Guide to WordPress Custom Post Type For WordPress Beginners!

  • June 7, 2018 at 7:30 am
    Permalink

    Hello Sam, Thanks for this great stuff!

    I just added a new custom post type.

    But I am facing trouble when create a new post, I couldn‚Äôt find ‚ÄúPage Attributes‚ÄĚ option to choose template.

    Can you guide me to solve this issue?

    Reply
    • mm
      June 11, 2018 at 3:48 am
      Permalink

      Hello Vaishali,
      You need to modify the above PHP code updating the array with the below once:
      “`’supports’ => array( ‘page-attributes’ ),
      ‘hierarchical’ => true,“`
      to make that support for your site.

      Hope this helps! Do let us know! ūüôā

      Reply
  • June 19, 2018 at 7:52 am
    Permalink

    Hi, Sam thanks for this response I had the same problem. Now I can resolve this.

    Reply
  • October 28, 2018 at 3:52 pm
    Permalink

    Hi Sam,

    I’m using a Knowledgebase plugin that uses CPT’s for it’s articles. Everything is working just fine except the Colormag theme is displays these KB articles with No Category. Knowledgebase plugin does support categories and all the article are labeled with a certain KB-category. Any idea how I can show the KB-category the the KB-article?
    Thanks a bunch for your thoughts.

    Reply
    • mm
      October 30, 2018 at 5:31 am
      Permalink

      Hello Martyn,
      Regarding your query, I think it would be better if you pass it on to our forums or support pages. They can help you with an elaborate explanation as well as the solution. Here is a quick link: https://themegrill.com/support-forum/. Hope this helps ūüôā

      Reply

Leave a Reply

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

Pin It on Pinterest