As you know, by default, WordPress comes with a few default post types, such as Post, Page, Revision, and Attachment. On a small scale, they will represent your blog topics. If you want a more variety of suitable post types like a post type for products, books, musics you have to create them yourself. We call these Custom Post Types.
In this article on WordPress custom post types we’ll explain what they are and then show you how you can create your own custom post types without any plugin. So Let’s go!
What Is a WordPress Custom Post Type?
WordPress custom post types are powerful features which used to convert your WordPress from a humble blogging site to a content management system(CMS). As the name suggests, you can use your custom made post types to create various types of content for your website.
By default, WordPress includes several different post types such as Post, Page, Revision, and Attachment. On a small level, they will represent your blog topics just fine. However, they might not be able to after your content grow more with many different topics.
That’s where the custom post type show its use. In short, it’s an additional post type that you can create based on your needs. With it, you’ll be able to classified your posts more specifically.
You can easily create your own custom post types. These will useful when creating posts that has a different format than a standard post.
For instance, if you run a News website, then you would probably want to create a News post type. You can also create custom post types for portfolios, testimonials, and movies.
Let’s Create our Own Custom Post Type in WordPress
Now let’s start creating the function to create your own post type. In my example, I’m going to create a post type called “Movie”, as this is for an movie reviews site.
Add below code to your themes functions.php
function wbg_movie_post_type() { } add_action( 'init', 'wbg_movie_post_type' );
This function will contain all of our code for custom post type. You can change its name to add a prefix of your own instead of wbg_. It is a good practice to use a prefix on all of your functions.
By using that you your function can’t clash with the name of any existing function provided by your theme or the other plugins you’re running on your website.
The rest of the code will be added inside this function.
Defining the Labels for Custom Post Type
Now it’s time for defining the labels for our custom post type.
$labels = array( 'name' => __( 'Movies', 'text_domain' ), 'singular_name' => __( 'Movie', 'text_domain' ), 'add_new' => __( 'Add New', 'text_domain' ), 'add_new_item' => __( 'Add New Movie', 'text_domain' ), 'edit_item' => __( 'Edit Movie', 'text_domain' ), 'new_item' => __( 'New Movie', 'text_domain' ), 'view_item' => __( 'View Movie', 'text_domain' ), 'view_items' => __( 'View Movies', 'text_domain' ), 'not_found' => __( 'Not found', 'text_domain' ), 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), 'all_items' => __( 'All Movies', 'text_domain' ), 'archives' => __( 'Movie Archives', 'text_domain' ), 'attributes' => __( 'Movie Attributes', 'text_domain' ), 'insert_into_item' => __( 'Insert into Movie', 'text_domain' ), 'uploaded_to_this_item' => __( 'Uploaded to this Movie', 'text_domain' ), 'featured_image' => __( 'Featured Image', 'text_domain' ), 'set_featured_image' => __( 'Set featured image', 'text_domain' ), 'remove_featured_image' => __( 'Remove featured image', 'text_domain' ), 'use_featured_image' => __( 'Use as featured image', 'text_domain' ), 'menu_name' => __( 'Movies', 'text_domain' ), 'filter_items_list' => __( 'Filter Movie list', 'text_domain' ), 'filter_by_date ' => __( 'Filter by date', 'text_domain' ), 'items_list_navigation' => __( 'Movies list', 'text_domain' ), 'items_list' => __( 'Movies list', 'text_domain' ), 'item_published' => __( 'Movies published', 'text_domain' ), 'item_published_privately' => __( 'Movies published privately', 'text_domain' ), 'item_reverted_to_draft' => __( 'Movies reverted to draft', 'text_domain' ), 'item_scheduled' => __( 'Movies scheduled', 'text_domain' ), 'item_updated' => __( 'Movie updated', 'text_domain' ), 'item_link' => __( 'Movie link', 'text_domain' ), 'item_link_description' => __( 'Movie link description', 'text_domain' ), 'name_admin_bar' => __( 'Movies', 'text_domain' ), 'update_item' => __( 'Update Movie', 'text_domain' ), 'search_items' => __( 'Search Movie', 'text_domain' ), );
Defining the Arguments for Your Custom Post Type
$args = array( 'label' => __( 'Movie', 'text_domain' ), 'labels' => $labels, 'description' => __( 'Movie Description', 'text_domain' ), 'public' => true, 'hierarchical' => false, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-editor-video', 'capability_type' => 'page', 'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats'), 'taxonomies' => array( 'category', 'post_tag' ), );
Let’s take a look at what all of these do.
- ‘label’ => Name of the post type shown in the menu. Usually plural. Default is value of $labels[‘name’].
- ‘labels’ => An array of labels for this post type we already defined. See get_post_type_labels() for a full list of supported labels.
- ‘description’ => A short descriptive summary of what the post type is.
- ‘public’ => Whether a post type is for use publicly via the admin or by front-end users. Default is ‘false’.
- ‘show_in_menu’ => Show post type in admin sidebar. To work, $show_ui must be true. Default is value of $show_ui.
- ‘show_in_nav_menus’ => Show post type in <a href=”https://codex.wordpress.org/Navigation_Menus” target=”_blank”>Navigation Menus</a>. Default is value of $public.
- ‘show_in_admin_bar’ => Show post type in admin bar. Default is value of $show_in_menu.
- ‘menu_position’ => post type position in the menu. To work, $show_in_menu must be true. Default null (at the bottom). For all menu postitions see below.
- ‘menu_icon’ => Post type icon. Use dashicon name or full icon URL (http://…/icon.png).
- ‘capability_type’ => The string to use to build the read, edit, and delete capabilities. Default ‘post’.
- ‘supports’ => Core feature(s) the post type supports. Such as ‘title’, ‘editor’, ‘comments’, ‘revisions’, ‘trackbacks’, ‘author’, ‘excerpt’,
- ‘page-attributes’, ‘thumbnail’, ‘custom-fields’, and ‘post-formats’. Default is an array containing ‘title’ and ‘editor’.
- ‘taxonomies’ => An array of taxonomy identifiers that will be registered for the post type. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type() .
The position in the menu order the post type should appear:-
- 5 – below Posts
- 10 – below Media
- 15 – below Links
- 20 – below Pages
- 25 – below comments
- 60 – below first separator
- 65 – below Plugins
- 70 – below Users
- 75 – below Tools
- 80 – below Settings
- 100 – below second separator
Adding the register_post_type() Function
The final step is to pull all of this together into the register_post_type() function.
Below your arguments, and still inside the braces, add this:
register_post_type( 'movies', $args );
This registers the ‘movies’ post type, with the arguments we’ve already defined.
Now save your file. You’ll find that a new post type has appeared in the admin menu sidebar.
The movie post type is now registered and ready for to use on our site. Take some time to add some posts to it and then we’ll look at how you can display your posts from your custom post type on the front end of your site.
Wrapping Up!
Now you have now learned how you can create a Custom Post Type in your WordPress site, which can be easily done by following the steps mentioned in this blog.
If you have any queries or suggestions regarding the blog, feel free to comment below.