WordPress Custom Post Types: weapons of mass construction

WordPress Custom Post Types: weapons of mass construction

One of the things that has always attracted me the most to WordPress is the simplicity of its design. This simplicity has made it evolve from a simple content management system for blogging to a tool for building e-commerce sites, portfolios, learning management systems (LMS) or real estate portals.

And a large part of this flexibility is due to the possibility that WordPress offers us to create custom post types (CPT).

The post types in WordPress

If we look at the database on which WordPress is supported, we will find a table that, by default, is called wp_posts. In this table are stored all the contents that we will create in our work with WordPress.

By default, WordPress offers us a set of content types that generically are called post types. These post types are:

Posts, when its content is generally displayed sorted by its creation date (the blog itself) and, in addition, can be grouped by the type of content (taxonomies).

Pages, when their content is not displayed in relation to their creation date and a hierarchy can be established between them (parent page-child pages).

Attachments, which are media (images, videos or documents) that are linked to a post or a page.

Menus, which constitute the basic navigation system items on the website.

Custom CSS, which contains style sheets for the various interfaces of the website.

There are some other predefined types but they are not important for the purposes of this article.

Each of these post types has a series of fields, sort of like boxes, that store the contents of each post in an organized way. For example, a blog post has fields to store the title, the content of the article, the date of publication, the author, and so on.

If, in addition, we need to create some more fields, WordPress allows us to associate one or more custom fields to the post.

In the post type posts we can group homogeneous content using taxonomies. WordPress offers two types of taxonomies: hierarchical, also known as categories, in which a category can have subcategories, and non-hierarchical, also known as tags, which maintain a single level of content grouping.

The great leap of WordPress: custom post types

The simplicity of WordPress design has left it open so that, in addition to the default post types, we can also create our own custom post types, giving our website unlimited possibilities.

In fact, many popular plugins are based on the creation of custom post types. For example, WooCommerce is based on the creation of a custom post type which is the "product". If we look at the content of a WooCommerce product, we will also see that it contains a lot of custom fields (price, tax, inventory, SKU...).

WooCommerce: Product configuration page

On the other hand, let's go that we can group WooCommerce products by categories and assign tags to them. This means that we can also create custom taxonomies.

In short, in WordPress we can create custom post types with custom fields and that are associated with custom taxonomies. But there is still more.

Establishing relationships between custom post types

Let's imagine that we are developing a CRM with WordPress. For this we will have created two custom post types:

Leads, where we are going to store all the lead data (name, email, phone, company, position...).

Actions, where we will store all the actions that we have performed with a particular lead (calls, meetings, emails...). This custom post type will store the actions of ALL the leads that we have registered in our CRM.

If we want to display a record of the lead with all its information and the actions we have performed on it, we will need to create a relationship to retrieve and display only these actions. Therefore, we will establish a "one-to-many" relationship so that for each lead we can discriminate the actions performed just on it.

There are other types of relationships: one-to-one and many-to-many, which allow us to develop other functionalities.

With all this we have at our disposal a complete arsenal of resources to develop the most complex applications.

Relationships Wizard in Toolset


Working with custom post types

There are several ways to create the resources we have talked about.

The first one is to program the functions that will create these custom post types and insert them in the WordPress functions.php file or, better, in a code insertion plugin such as WP Code or Code Snippets.

However, creating these resources is not an easy task but we can rely on an external tool such as Hasty.

Hasty Custom Post Type Generator interface

Alternatively, we can use plugins that will solve the problem. Next, I describe the most popular ones:

  • Pods (free): Among the free ones, it is the most complete. It takes a little work to get used to its use but it works perfectly in most cases.
  • ACF (free and Pro): It is one of the most popular. Until a few versions ago it was only able to handle custom fields and required another plugin (usually CPT UI) to create custom post types. Today it already incorporates both functionalities.
  • Toolset (premium): It is really a very complete plugin suite that allows extremely complex applications and incorporates a complete user control. Although its interface is a bit old-fashioned, it is one of my favorites.
  • Jet Engine (premium): If you work with Elementor, this is your plugin. As powerful or more than Toolset, it covers quite spectacular functions.
  • MetaBox (free and Pro): Although I have not worked with it, it covers all the functions described above.

The art of custom post types

Apart from the tools we have mentioned, the creation of custom post types and related resources, although simple, requires a very important planning.

All these resources are intimately linked to the data model of the application we want to develop and, consequently, they must be subject to a previous analysis before starting to create them. Let us not forget that failures in data models are costly to correct.

Custom Post Types should be defined by the Data Model

It is therefore advisable to visualize what data we are going to display and how before designing the resources needed to store these contents.

Custom post types are a revolution within WordPress that will allow us to build very complex applications in an almost no-code context.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了