In this article, we will explain what custom post types are. In addition, we will show you how to create custom post types and how they can help in organizing and adding your website content. We will also add custom fields using the Types plugin. Moreover, we will demonstrate translating custom post types using WPML.
What are “Custom Post Types”?
Pages, posts, attachments, revisions, and navigation menus are the default WordPress post types; but what does that even mean? To be more clear and specific we can refer to “Custom Post Types,” also known as CPTor “Custom Content Type.” For example, Pages are pieces of content of the post type “page”; similarly, Posts are pieces of content of the post type “post.”
Let us say that you have a website that reviews games. You will probably want to add reviews for more than one game. All games share some information (metadata) like release date, genre, platform, system requirements, screenshots, etc. In this case, you will want to create a custom post of type “game” and add custom fields to include the metadata. Other examples of custom post types could be Portfolios, Testimonials, and Products.
Once you have created the custom post type game, it will show up on your WordPress dashboard as a separate menu item. You can also use the default categories available or create custom taxonomies suitable for the content. In addition, you will have the opportunity to choose how the custom post type will be displayed on the front-end. Here, you can read more about Post Types.
Why should I use “Custom Post Types”?
The general idea behind using custom post types is to make adding and displaying some specific pieces of content with the same structure or format easier. There are many indications of when you should start using a custom post type:
- When you do not need these pieces of content to have a chronological hierarchy;
- Additional fields are needed to add more details about the post;
- You want to display your posts in a specific way on the front-end;
- More categories and tags are needed to classify the pieces of content.
Creating custom post types and custom taxonomies will make it easier for your users to browse and sort your custom posts. For example, games can be sorted and searched by ( genre, release date, etc).
The CPTs allows you to have pretty permalinks as they will show the custom post type name and the post name. For example, the URL for a Game post will be https://mysite.com/game/game-name
How to create “Custom Post Types”?
It is easier to create Custom Post Types using a plugin; however, you can also add some code to your functions.php theme file to add a Custom Post Type and custom fields to the theme you are using.
You can add the code below to the functions.php file of your theme to create a custom post type; in this example, we will create the post type game. Note that you can add this code to the end of the functions.php file.
Indeed, this is the “geeky” way to create custom post types. An easier method is to use a plugin such asTypes. It is a free plugin that will allow you to create your own CPTs, custom fields, and custom taxonomies, effortlessly. In addition, Types documentation provides clear and detailed instructions to create your custom post type in a few minutes. We will use it to add the custom fields to our custom post type Game. You can also check the full documentation of adding custom fields.
After you install and activate Types, a new menu item will show on your WordPress dashboard. Follow the instructions below to add our custom fields to the CPT Game:
- Navigate to Toolset -> Add New Post Field Group.
- Click on the Add New Group button.
- Fill the Name field and click on the Save Field Group button.
- Assign the post field group to the CPT Game by clicking on the Edit button found on the “Where to include this Field Group” section and choose Games as illustrated below:
- Click on the Add New Field button and choose the field type. For example, we will create the custom fieldsrelease date and genre. We will choose the field type Date for the release date. Finally, we will set the field options and click on the Save Field Group button.
- To create the genre custom field, click on the Add New Field button and choose the field type to be Single Line. Set the field options and click on the Save Field Group button.
This is how our CPT Game will look on the WordPress dashboard:
You can use the Views plugin to display the Games CPT on your front-end. Views documentation will provide you with the information you need to create content templates to display your custom content type they way you want. Here is an example of how the Games CPT will be displayed on the front-end using the Views plugin:
How to translate “Custom Post Types”?
With WPML you can translate the CPTs that come with your theme, CPTs that you have created using a plugin, and CPTs that you have coded as shown in this tutorial. To set the CPT (Game) that we have created for translation, you will need to install and activate WPML’s core plugin (sitepress multilingual cms) along with the Translation Management add-on.
- Navigate to WPML -> Translation Management and click on the Multilingual Content Setup tab and scroll down to the Custom Posts section.
- Set the CPT Game to be translated.
- Click on the Save button.
- Go to the Custom Field Translation section and set the custom field genre to translate and the release date to copy from original to translation since we don’t need to translate the date.
- Click on the Save button.
- Navigate to Games -> All Games. Click on the + icon to add your translation.
- Click on the Copy content from English button.
- Add your translated text and click on the Publish button.
Using CPTs makes it easier to manage your content. If you are running a multilingual website, you will want to translate your CPTs, custom field type, and custom taxonomies. WPML can translate all these elements efficiently.