Using Custom Post Types in WordPress – .com

In today’s tutorial we will see what WordPress Custom Post Types are, their use, what we should use them for, how to do it and the best practices.

What are Custom Post Types (CPT)?

When WordPress started, only “posts” existed. Nothing more. There were no pages, only posts, because WordPress was only good for blogging.

But over time its developers saw very clearly that we needed to go further. And they created the “Pages”, which are a “kind of post“, or to put it another way, a “post type”. That’s where the “Post Type” comes from. Surely it would have made more sense to call him “Content Type“, that is, “Content Type”but that’s how it stayed.

The pages, then, are like posts, with some differences. For example, they have no date, no categories, and no tags. If you want to know more you can read the tutorial, in which I tell it in more detail.

So, we already had Posts and Pages (type of post). But the real revolution came with WordPress 3.5, which brought the “custom Post Types”. And what is it about? Well, neither more nor less than the possibility of creating your own post types. With others words, “Custom Post Types”.

And that’s what CPTs are: New content that we can add to WordPressin addition to the Tickets (posts) and the Pages (post types).

Examples of Custom Post Types (CPTs)

An example of CPT would be the “Products” of an eCommerce. If you use WooCommerce, you will see the “Products” menu in the control panel. That is a CPT. But we could put any other type of content: Services, people, movies, contacts, animals…

For example, here you can see a screenshot of my control panel:

Specifically in my case, I am using 6 of them, let’s review:

As you can see, anything goes when it comes to CPTs. Audios, videos, articles, courses, services… All well organized.

And maybe some of you will ask “And couldn’t you use categories?” Which leads to the next question… When is it worth doing a CPT? Well, I’m glad you ask this question.

When is it worth doing a CPT?

If you have ever considered the structure of a website, surely at some point you will have considered what categories and labels to use. Or even if you want to use categories and tags. Or even if you use CPTs. When should we use one thing or the other?

See also  574. Business idea: Used cardboard boxes - .com

The answer lies in the similarity of those CPTs with the pages and posts. If they share the same type of information or organization system, it doesn’t make sense to do CPTs.

For example, I could “pop” my podcast episodes as posts. But I consider them “distinct” enough to make them independent. Podcasts, for example, have a custom field in which there is the MP3 of the episode. That field does not make sense that it is in the “Entries” or in the “News”, for example. Hence I keep them apart.

Or for example, my services would not make sense if they were in a category called “services” in Entries. Hence, I have them separately, since they have their own fields, such as the price, or the payment method.

Also, that allows me to have a flawless link structure:

  • The courses -> https:///cursos/
  • Tutorials -> https:///tutoriales/
  • The news -> https:///noticias/

On the other hand, if they were post categories, the URLs would not be so pretty, since they should have the /category/ directory in the middle. You have more information about this in the tutorial.

And I can even layout each file page of each CPT in a different way. For example, here we see the episodes of the podcast:

And here is the layout of the tutorials, like this one, that you are reading right now, in another format:

So I advise using CPTs instead of categories when the content entity is significant enough to stand alone.

Well, let’s suppose then that you have analyzed your case and that you want to use CPTs for something specific. Now the key question is… And how do I create a CPT?

How to create a Custom Post Type

There are several ways to do it, but the most normal is through one of these 3, ordered from simplest to most complex:

Through a plugin

There are plugins that have their own CPTs. For example, WooCommerce.

As you already know, WooCommerce is an eCommerce plugin for WordPress. And as we have said before, when you install it, the CPT “Products” appears in the control panel.

See also  Reaper course - .com

That is the simplest way, since we don’t have to do anything, just install and activate the plugin, and that’s it. Magically we already have that CPT in our WordPress installation. Let’s note that it is an eCommerce plugin, that its purpose is not to create CPTs in itself, but that these are only an effect of them. But we have another option.

Through a CPTs creation plugin

If right. There are certain plugins that are used to create CPTs. Although there are many of them, I recommend, from the people of WebDevStudios, which are WordPress cracks. 100% recommended.

What this plugin does is neither more nor less than offer us a graphical interface through which we can create those CPTs and then use them in our day to day.

In other words, simply by filling in all the options, we can create our own CPTs that will be added to our WordPress and we can use in our day to day to add content. If you want me to prepare a tutorial telling you how to do it step by step, tell me through the support tab of the , and I will do so.

Although this method is more flexible than the previous one, because we create and define the CPT as we want (name, slug, properties, fields it supports, if it has files, if it is visible, etc)It’s not my favorite for several reasons.

  • First of all because we have to have that plugin always installed and active to use the CPT (if we uninstall it, the CPT disappears).
  • Secondly, because it affects the performance of the web itself, as it is just another plugin in our installation, and it all adds up.
  • And thirdly, because if we leave that menu visible, it is very likely that sooner or later someone will end up fiddling with it, and probably breaking something. (Murphy’s law).

And finally, the best option. My favorite and the one I always recommend, both for developers and for “Juan Palomo” who like to learn and do things as well as possible.

via code

Making a CPT by code is very simple. In the del class you can see it step by step through a video tutorial, and you can also download sample codes. But to give you an idea, only these lines would suffice:

See also  Tax course for self-employed #8. VAT Presentation (Form 303) - .com

add_action( ‘init’, ‘create_a_cpt’ );
function create_a_cpt() {

$args = array(
‘public’ => true,
‘label’ => ‘Books’
);
register_post_type( ‘book’, $args );
}

Only with this we have already created a CPT called “Books” that will appear in our control panel automatically, ready to be used. We don’t need to do anything else.

Obviously we can complicate it much more, and indicate many more options and settings, especially if we want to customize it to our liking. But these few lines would be enough to have it running.

Important: CPTs are NOT a matter of themes

At this point, I want to make an important notice: CPTs are not a matter of themes. I repeat: . That’s a plugin thing.

And I am very radical in this matter for a simple reason: What comes in a theme, goes with the theme. This means that if we are using CPTs that have come with a theme, the day we change that theme for another (and believe me, sooner or later it will happen)those CPTs They will disappear.

I tell you in detail in the tutorial, and also in this video that I recorded more than a year ago (in which, by the way, he had a bad cold):

And that is why we should never trust themes that have functionalities, or CPTs. A priori it may seem very nice, but in the long term it is a very bad decision.

Summary and conclusion

Custom Post Types are custom content entities that we can add to WordPress to better organize and structure the content of our website.

Sometimes they are given to us through plugins and sometimes we create them, either through a plugin or through code. They should never come with a theme.

If you want to know more, I recommend the , in which we see all this and much more, as well as the , which complements these perfectly.

Finally, this is it. For any doubt, question or suggestion, you can contact me through the support tab of the . See you in the next tutorial!

Loading Facebook Comments ...
Loading Disqus Comments ...