WordPress Custom Post Types and Taxonomies: Introduction Redefined

May 31, 2017 By - Usama Muneer | 239

    Introduction

    Out of the box, WordPress has several post types already defined. The most common ones we interact on the daily basis are posts and pages. So let’s first take a look at what posts and pages are, because this will be instructive when we create our own custom post types.

    WordPress Posts

    POSTS are what you normally create on WordPress. They are organized as reverse chronological order so that the last post goes first in the index and the rest you can see is below that. They are non-hierarchical, means that posts may not have a parent/child relationship. Instead posts are created and managed by the Authors & they have categories & tags. In other words, Every post must have a category and some tags.

    WordPress Pages

    In contrast, PAGES are single individual items. They can be displayed in parent/child relationship & we usually see this on the main menu. Pages don’t have indexed pages and they don’t have categories & tags. Only way to get to them is through a direct link that’s why they usually appear on the main menu.

    POSTS are normally used for some recently updated items, such as some article or news update etc. whereas, PAGES are static content, which rarely updates.

    Custom Post Types

    Now let’s dig into the Custom Post types. When you create a custom post type, you can make them act like either a post or a page & which ever you choose. That custom post type will inherit all the features of it. They can be organized by categories, tags or custom taxonomies. They have separate index page and can have custom index template. They can have all of, or a combination of, or none of the standard features. Custom post types can be called anywhere using a custom loop.

    Before digging deeper into the custom post types, let’s first understand what Taxonomy means.

    Taxonomy is the schema of classification. It could be defined as the organizational system that allows us to relate one item with another item with other similar items in hierarchical and non-hierarchical groups

    Example

    Let’s understand it by a simple real life example;

    In your Wardrobe, you have your shirts in one section, your jackets in another, your pants in another and so on. But whats the benefit of using this technique? Let’s say you want to find some pant, you just go to the section containing your pants only. Simple enough? These are hierarchical system or categories, a type of taxonomy. And every item have some tag on it, like its size, color and so on, these are named as tags, which are non-hierarchical.

    WordPress Taxonomies

    In WordPress, we have two default taxonomies, that you normally use all the time. The first one is category, every post must have a category. They are displayed as hierarchical system, so that means we can have parent/child relationship. Moreover categories are the main organisational method within WordPress.

    On the other hand, tags are optional. You don’t have to use them, but you can use them whenever you want, they are non-hierarchical, meaning that they are not related to one another & they are considered as the secondary organizationals method in the WordPress.

    When you’re creating custom taxonomies, you can either create hierarchical(category) or non-hierarchical(tags). These taxonomies can be applied to one or several post types including posts. Custom taxonomies can have custom index and archive template. These are usually displayed along-side categories & other meta content. Custom taxonomies allows for more advanced Organization of content.

    Creating a new Custom Post Type & Taxonomy

    So, When you’re creating a new custom post type or taxonomy, the very first question arises in your mind is where should you write your code in WordPress theme structure.

    Well there are two options, one is to create a simple plugin, & other is to write it into your functions.php. Now, there are benefits and drawbacks of both, such as,

    A Plugin can be turned on and off, and it could be specific to each theme you’ve created. This technique is used in most of the cases.

    Whereas, on the other hand, if you write your custom post type and taxonomy code in your theme directly, it is only for custom builds & its content is theme dependent & allows for some advanced functionality.

    It’s completely one’s individual choice of how to use it in your theme. In the next article I’ll explain how to create custom post type & taxonomy using both described techniques & we’ll find out more about custom post types and taxonomies.

    If you have any questions, write me a comment below or find me at twitter.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Laravel Lahore meetup has ended on Friday & we got a lot to learn from the top-notch of speakers & influencers.

    This is the third instalment of the series of Laravel awareness meetups before the main Laracon event. The Laravel Live PK, which will be a great milestone while helping our fellow web developers across the city to become motivated and help them grow as Laravel developers.

    Well, assume we're having a User and Admin classes, which are required to use some methods with the same functionality. We'll look on how do we achieve this using both Interfaces and Traits. 

    Traits are simply the way to share methods among classes in your application while keeping an equal eye on DRYing up code and making sure its re-usability in the application.