WordPress Post Types: An Abstraction For Entities

February 8, 2017

When programmers talk about entities, they are usually talking about a concept or something that has an existence within a system. Sometimes it’s a class, sometimes it’s a library or dependency, or it may be something like WordPress post types.

If WordPress is to be thought of as an application foundation (versus a framework), it’s important to think about certain features in ways that can be treated as entities. In the example above, a post type is just that.

When custom post types were introduced, it gave us a way to introduce something other than pages and posts into the application. Now we can implement any concept – like an event – and treat it as its own entity within the system.

But when all is said and done, it’s all saved in the posts table. And that’s fine because software development is all about abstraction. As we implement WordPress post types, we’re implementing an abstraction on top of the idea of posts.

WordPress Post Types

In the last post, I walked through the process of creating a user that will ultimate be associated with some entity in the WordPress application.

Perhaps s/he is the author of a post,

Perhaps s/he is the seller of a product,

Perhaps s/he is the narrator of a story,

Or whatever else you can imagine.

The possibilities are endless. But before we talk about how to create an association between the two entities – that is, the user and the WordPress post type – we need to talk about how to implement an entity or a custom post type programmatically.

There’s a lot of documentation on this, and there’s a lot of tutorials available on this, so I’m not going to belabor the point. Instead, I’m going to try to keep this as barebones as possible. Remember, the goal of this whole series is to ultimately get to a point where we’re associating entities via metadata.

But before doing that, we need to create the entities. Thus, we’ve got a user so let’s do that.

For our example, let’s say the user we created in the previous post, Meghan, is the author of a book. And within a WordPress installation managed by someone else, there’s custom post type for books each of which has a few attributes:

title,

description,

publication date,

ISBN,

number of initial copies sold,

where it can be purchased,

and so on.

You can add more or less to your implementation. In fact, perhaps you’re in the business of selling eBooks and the book’s URL for purchase is one of the attributes. Whatever the case, you get the idea.

Implementing a Post Type

If you read the documentation on creating custom post types, there’s nothing abnormal about this. It should look exactly as you’d expect.

Regarding where you create this depends on the nature of your project. There are a lot of people who will make a case for it being in a plugin or being baked into a theme. This isn’t the place to debate that. Suffice it to say that, for my purposes, I’m assuming it’s all part of the same plugin from the previous tutorial.

Furthermore, I’m not digressing on custom taxonomies, comments, or any of the other custom type of data that’s possible to associate with a custom post type.

From there, we then take a look at what this looks like in the WordPress administration area as well as what this looks like in the database.

Look Behind The Curtain

First, here’s how it looks (or should look) in the WordPress administration area:

And if you look at the database after creating an entry, you’ll see something like this:

Obviously, I’ve limited the records returned in the database front-end by filtering the results to make the screenshot easier to follow.

On The Books

And that’s it, really. I mean, at the most fundamental level this is how you create a custom post type within WordPress. I know it’s nothing new, but if I could make a slightly stronger case for a post like this, it would include the following two points.

1. It’s About Abstractions

If you’re looking to get into building web applications using WordPress, it’s important to stop thinking about things in traditional terms like posts, pages, comments, and categories, and start thinking about them as more generic abstractions.

Entities, as mentioned earlier in the article. Once you do, you’ll have a shift in the mental model that drives your ability to think through and implement a solution.

2. It’s About Relationships

Though we often think about things in their individual parts such as posts and pages, we inherent know that posts can have comments, comments can have replies, pages can have templates and so on.

There are relationships between all of these individual things that are part of the system. And when you start to think about the relationships as being something that allows us to do more, you begin to see just how powerful something can be. It’s whole “the whole is greater than the sum of its parts.”

So once you have individual entities in place and once you start seeing how they can relate to one another, you’re taking a step up in creating something that solves a slightly more complex problem from seemingly simple parts.

And that’s where we’ll head next.

What About The Metadata?

The whole purpose of this series is walking through how to create an association between entities, right? Specifically, we’re looking to create an association between a user and a post or, more specifically, an author and a book.

WordPress provides the foundation for doing this and it this is where the power of additional tables and hooks can come into play. In the next post, I’m going to talk about how to create this association, the directions the associations can go, and how to work with this data if, say, a user is deleted from the application.

Just Getting Started with WordPress?

I write a lot about WordPress development but if you're just getting started, I recommend checking out WPBeginner. They have free training videos, glossary, and more.

WPSessions hosts some of the best WordPress training you’ll find anywhere from many well-known speakers.

If there’s something you’d like to learn, and it’s not already covered here, it’s probably been covered at WPSessions. If you use the special link below you can watch your first session for free and get a steep discount on the full-access VIP membership.