What is a custom post type in WordPress?
A custom post type is a content model like posts and pages. As WordPress has evolved from a blog to a content management system, the term 'post' remains. However, it can refer to any type of content in WordPress. By default, there are the following post types:
- Post
- Page
- Attachment
- Revision
- Navigation menu
You can create any post types and name them as you wish. For example, if you run a website for film reviews and presentations, you may need a post type for films. These post types can have their own sections and categorizations, such as portfolio, testimonials, products, etc.
Creating custom post types with a plugin
The easiest way to create post types in WordPress is to use a plugin. Since this method is simple and safe, it is recommended for beginners.
First, install and activate the plugin āCustom Post Type UIAfter activation, the plugin adds a new item called CPT UI to the administration menu.
To create a new post type, go to CPT UI Ā» Add New.
The 'Custom Post Type UI' plugin also allows the creation of custom taxonomies.
Therefore, the Custom Post Type page is divided into two columns. On the left is a form for setting up the custom post type, and on the right for creating taxonomies.
In the column for custom post types, you must first set a name. This should not exceed 20 characters, for example, Movies, Recipes, Offers, etc.
In the next step, you will set a label for the post type. This label will be displayed in the WordPress admin area. For clearer meaning, use the plural form of the term, e.g. Movies, Glossary, etc.
Finally, write a description for your custom post type. This should simply explain the purpose for which the post type was created.
Then click the 'Create Custom Post Type' button to create the new post type. Done.
You can also click the links 'Advanced Label Options' and 'Advanced Option' to change further details about the post type.
Creating custom post types manually
The problem with creating post types using plugins is that when they are deactivated, all post types are no longer displayed. All content of the individual posts remains intact, but the post type is no longer visible in the admin area.
If you are working on a client's website and do not want to install a plugin, you can add the custom post type using the following code in the functions.php file of your theme.
Here is a quick and complete example so you can understand how it works:
function create_posttype() {
register_post_type('movies', [
'labels' => [
'name' => __('Movies'),
'singular_name' => __('Movie')
],
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'movies']
]);
}
add_action('init', 'create_posttype');
This code registers a post type called 'movies' with a range of arguments that define the options for the post type.
Here the details of the options are explained:
function custom_post_type() {
$labels = [
'name' => _x('Movies', 'Post Type General Name', 'twentythirteen'),
'singular_name' => _x('Movie', 'Post Type Singular Name', 'twentythirteen'),
'menu_name' => __('Movies', 'twentythirteen'),
'parent_item_colon' => __('Parent Movie', 'twentythirteen'),
'all_items' => __('All Movies', 'twentythirteen'),
'view_item' => __('View Movie', 'twentythirteen'),
'add_new_item' => __('Add New Movie', 'twentythirteen'),
'add_new' => __('Add New', 'twentythirteen'),
'edit_item' => __('Edit Movie', 'twentythirteen'),
'update_item' => __('Update Movie', 'twentythirteen'),
'search_items' => __('Search Movie', 'twentythirteen'),
'not_found' => __('Not Found', 'twentythirteen'),
'not_found_in_trash' => __('Not found in Trash', 'twentythirteen')
];
$args = [
'label' => __('movies', 'twentythirteen'),
'description' => __('Movie news and reviews', 'twentythirteen'),
'labels' => $labels,
'supports' => ['title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'],
'taxonomies' => ['genres'],
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page'
];
register_post_type('movies', $args);
}
add_action('init', 'custom_post_type', 0);
This code adds a variety of options such as support for comments, custom fields, etc. There is also a taxonomy called 'genres'. Change hierarchical
to true
, to enable parent-child relationships similar to those on pages.
Display of custom post types on the website
By default, WordPress can display custom post types. Once some are created, you can have them displayed.
Display with default archive template
Go to Appearance Ā» Menus and add a link to your menu. It will refer to your custom post types, typically in this form:
- http://example.com/movies
- http://example.com?post_type=movies
Save the link in the main menu and click on it on your website. This will take you to an archive of your custom post types. To change the appearance, modify the file archive.php
of the theme.Ā
Using templates for archives and single posts
You can create special templates. Create a file named archive-movies.php
in the theme directory and copy the content from archive.php
into it to make adjustments.
Create a file for single posts single-movies.php
and copy the content from single.php
into it to make adjustments.
Display custom post types on the homepage
Add the following code to the file functions.php
Enter your theme to display custom post types on the homepage as well:
add_action('pre_get_posts', 'add_my_post_types_to_query');
function add_my_post_types_to_query($query) {
if (is_home() && $query->is_main_query()) {
$query->set('post_type', ['post', 'movies']);
}
return $query;
}
Querying post types
Use the following code for specific queries in your templates:
// Query args
$args = ['post_type' => 'movies', 'posts_per_page' => 10];
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
echo '' . get_the_title() . '
';
echo '' . get_the_content() . '';
endwhile;
wp_reset_postdata();
else :
echo '' . __('Sorry, no posts matched your criteria.') . '
';
endif;
Display in widgets
As you have already seen, WordPress comes with standard widgets like the display of the latest posts. However, these widgets do not allow the display of post types. But what do we do if we want to display these post types by the latest? There is a simple solution for this.
First, you need to install and activate the Ultimate Post Widget plugin. After activation, go to Appearance >> Widgets and add the Ultimate Post Widget to your sidebar.
This powerful widget allows you to display all posts regardless of their type. You can also show the beginning of the post along with a 'Read more' link next to its image and title. Customize the widget according to your preferences and the post types you want to display. Then save the changes and see the result on your website.
More tips for custom post types
There are many things you can do with post types. You can display them in your RSS feed or categorise them by their feed. If you are using the Disqus comment system, you can also make the appropriate settings in WordPress.Ā
We hope this article has helped you create your desired custom post types. If you have any questions or useful tips for improving post types, please let us know in the comments section.