WordPress custom post type support

WordPress custom post type support explained

Those of you who have developed code in WordPress environments surely heard and used WordPress custom post type support to create and extend the default WordPress post types. In a quick WordPress structure run trough, we know that everything is a post type in WordPress.
Anything starting from posts themselves to pages, categories and other taxonomies.
In order to fully grasp and understand the term, post types you need to dive in a bit and pay attention to the following explanation and examples, bear with me and I promise that after you will finish reading this article you will know what a WordPress post type is and a WordPress custom post type.

WP Post Types

Let’s begin with a short quote from the official WordPress documentation :

WordPress can hold and display many different types of content. A single item of such a content is generally called a post, although post is also a specific post type. Internally, all the post types are stored in the same place, in the wp_posts database table, but are differentiated by a column called post type.

What can we understand from these few lines? Well, we can grasp that WordPress supports a different variety of content types.
I will go further and tell you that WordPress groups them together under one “Leader” aka the wp post types.
For example, posts under POSTS,POST TYPE pages under the PAGE POST TYPE and so on.

WordPress default post types

Let us have a quick view of WordPress default post type before we try to understand custom post types:

  • Post (Post Type: ‘post‘)
  • Page (Post Type: ‘page‘)
  • Attachment (Post Type: ‘attachment‘)
  • Revision (Post Type: ‘revision‘)
  • Navigation Menu (Post Type: ‘nav_menu_item‘)
  • Custom CSS (Post Type: ‘custom_css‘)
  • Changesets (Post Type: ‘customize_changeset‘)

First of all, if you want to go deeper into these post types please visit the WordPress Codex on wp custom post types.
For our reference, let’s take the post and page post types to try to understand.
As the above-mentioned explanation and quote say, WordPress houses different post types for different content types. the post post type groups all the single posts from the mentioned post post type together for various and different purposes. The same things happen with the page post type, except for one small and obvious difference. It groups PAGES.
So now that we understand a bit more about wp post types we can go ahead and enter our second phase of this article and dive in full strength into WordPress custom post type support.

WordPress custom post type support

So here we are, in the rest of the article we are going to cover and explain:

  1. What is WP Custom Post Type?
  2. Why Do We Need It?
  3. How Do We Make It Happen?
  4. Final Words.

Let’s get to it! So whats a WordPress custom post type?
Earlier we established that WordPress supports different types of content, in order to provide this support WordPress uses its post types to group single types of content under a bigger group and agenda, so we got the post types.
Why do you need it you ask? Well, think about What happens if you want to include a custom content type that WordPress doesn’t provide by default? Or let’s imagine you want to develop some sort of a new functionality for WordPress which isn’t supported in any of its generic post types. What do we do?
You guessed right, We create a post type of our own, this is where WordPress custom post type support comes into play.
Let us cover some ways and examples about the way we go on ahead and make it happen:
Wordpress custom post type support lets us do just that in the following manners.

  1. A use of a plugin ( like the famous CPT UI plugin ) to create and generate WordPress custom post types.
  2. Directly writing code to implement and create wp custom post types.

A Plugin

The first way is by means of a plugin, the most renowned one and the one I personally use myself on occasion is CPT UI.
Of course, there are a few more out there like Custom Post Type Maker and more.

Directly Writing Code (manually)

WordPress custom post type support is provided by WordPress. There is a whole mechanism of functions and calls integrated into WordPress allowing us the developers to create custom post types that will support our custom content, layout, and functionality.
Doing so will allow us to consolidate all of our singular custom content under one banner.
Let’s say for example that you are building a WordPress based website for a school. You would like to create a special kind of content for Teachers and Students and you want to display them in a certain manner that does not come by default in the existing wp post types.

This is where the magic happens

Using WordPress’s standard tools we can create a wide variety of content, layouts, functionality, and flexibility and more for each custom post type.
You can either write the custom code for the custom post type in the functions.php file or if we are talking about a big post type that you will want to customize its layout rather than to use the default WordPress single.php and archives.php  than you will have to create a whole file structure(templating system) for it.
BE ADVISED, If you will not create custom templating to display your custom content WordPress will automatically use archives.php and single.php to display content from  wp custom post types.
Furthermore regarding the last statement, if you will want to customize the layouts you will have to create a whole file system for that custom post type. Meaning, for example for posts we have the: single.php and category.php with fallback to archives.php.
In order to display our custom post type layout, we will need to create the layouts for them, the naming will be as follows:single-{post_type}.php category-{post_type}.php and so on.
Let’s dive into examples and finalize out understanding of wp custom post types:

function create_post_type() {
   register_post_type( 'cool_products',
      array(
         'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
         ),
         'public' => true,
         'has_archive' => true,
      )
   );
}
add_action( 'init', 'create_post_type' );

Lets Break It Down:
We create a function in which we register our custom post type. In the followed example you only see the minimal arguments needed to be passed to create a custom post type. For the full arguments list visit  register_post_type() Function Reference in WordPress Codex.
The next step is to hook our custom post type creation function with:add_action().
Congrats! you’ve created your very own custom post type that’s called cool_products!  You still have a way to go from this point, like templating, more arguments and options and so on, but at least now you can get started 🙂

conclusion

I hope you’ve enjoyed this on article on WordPress custom post type support and maybe even learned a thing or two.
like I said you have a long way to go and I would recommend reading more about the mentioned functions and references on this post.

WordPress custom post type support explained

Leave a Reply

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