Custom Post Types in WordPress is the feature which makes WordPress so popular content management platform. By default Post, Page and 5 others are post types available in WordPress. A few example of Custom Post Types are Portfolio, Project, Contact Form 7 or Product in WooCommerce etc.
You might need custom or additional post types for your own need, theme or plugin. Assigning WP default or custom categories or tags, generally saying taxonomies to your CPT is also a common requirement.
You might wish to have a different menu and page under WordPress dashboard to manage your own custom content. For example Movies, Testimonials and Books custom content types with custom fields to input post data.
Further, there might need a different archive page to display these post types. Exporting your taxonomies and custom post types is also a necessary step if you’re developing in a local environment.
The 6 steps article is an attempt to let you learn these process and smoothly apply within your theme starting from custom post types to exporting custom fields you have developed locally. The series of articles is divided into six parts and in this first part, we are going to setup Custom Post Type in our theme.
Create Custom Post Types in WordPress
Before we continue, let’s look what is included in all 6 steps, a series of articles:
Create custom post types
Exporting custom taxonomies/fields & further reading
There are few popular plugins to create additional post types along with custom taxonomies and fields for them. I have listed them at end of the article. If you are not a coding guy, you can install and setup CPT as per your own requirements.
To manually create custom post type, we need to use a function named`register_post_type()` in functions.php
file of our WordPress theme. I’m going to create Book post type.
function my_posttype_and_taxonomy() { $labels = array( 'name' => _x( 'Books', 'Post Type General Name', 'text_domain' ), 'singular_name' => _x( 'Book', 'Post Type Singular Name', 'text_domain' ), 'menu_name' => __( 'Books', 'text_domain' ), 'parent_item_colon' => __( 'Parent Book', 'text_domain' ), 'all_items' => __( 'All Books', 'text_domain' ), 'view_item' => __( 'View Book', 'text_domain' ), 'add_new_item' => __( 'Add New Book', 'text_domain' ), 'add_new' => __( 'Add New', 'text_domain' ), 'edit_item' => __( 'Edit Book', 'text_domain' ), 'update_item' => __( 'Update Book', 'text_domain' ), 'search_items' => __( 'Search Book', 'text_domain' ), 'not_found' => __( 'Not Found', 'text_domain' ), 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), ); $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => 'books', 'rewrite' => array('slug' => 'book'), 'description' => 'Book post type', 'menu_position' => 4, 'menu_icon' => 'dashicons-media-text', //'taxonomies' => array( 'category', 'post_tag' ) ); register_post_type( 'book', $args); //Function to add custom taxonomies //my_cpt_taxonomies(); } // Hooking up function add_action( 'init', 'my_posttype_and_taxonomy' );
Few Notes:
You need to change the term Book, Books or book with your custom post type name. Also $td
should have the name of your theme or plugin text domain. We have commented line #28 and #34 here. We will discuss these just in next tutorials of this series.
There are plenty of arguments to pass while registering post type with a default value if not supplied. Surely check all of these arguments at WordPress Documentation. If something doesn’t suit your requirement, don’t forget to assign the updated value to args
array.
The menu icon can be picked from WordPress Deshicons.
Note that if you rename the post type, your posts / taxonomy / custom field / slug etc. might get disappeared. In the last post in custom post types series, I have mentions how to safely rename CPT.
Now login to WordPress dashboard and check the post type Book is listed there.
Custom Post Types Plugins
In the next part, we are going to add WP default Categories / Tags taxonomies to these post type we created. But before that below is list of free Plugins from WordPress repository. Drive with them if you prefer to create custom post types, taxonomies and fields using a plugin:
These plugins present you friendly forms in WP dashboard. There you can select or specify required names and all the supported arguments for registering custom post types, taxonomies and fields. If you have liked the post then don’t forget to share the series to let others discover.
Read next: Add WordPress Taxonomies to the CPT.