WordPress has many different content types. A single item of content is called a post, also post is a specific post type. All the post types are stored in the same database table (wp_posts
) and differentiated by a column called post_type
. By default, five post types are available in WordPress.
Also, you can create new post types which are called Custom Post Types. A custom post type can be added to WordPress using register_post_type()
function. It very simple and you don’t need to use any plugin for that, you can register your custom post types without using Plugin.
In this article, we’ll show you how to create custom post types in WordPress. Also, you’ll know how to add taxonomy or category of custom post types in WordPress. WordPress custom post type helps you to divide up your website content in a more structured way. For example, your website offers some products to users and you want to separate the products section from the posts section. In that case, you would probably need a separate product post type. This custom post type will have its own custom category and custom fields.
The register_post_type()
function is used to create a post type and it only be invoked through the init
action. At the time of creating a post type, always register taxonomies of that post type. You can define and register a taxonomy using register_taxonomy()
function.
You only need to modify your current theme’s functions.php
file. Open the /wp-content/themes/theme_name/functions.php
file and place the following code in this file.
// Product Custom Post Type
function product_init() {
// set up product labels
$labels = array(
'name' => 'Products',
'singular_name' => 'Product',
'add_new' => 'Add New Product',
'add_new_item' => 'Add New Product',
'edit_item' => 'Edit Product',
'new_item' => 'New Product',
'all_items' => 'All Products',
'view_item' => 'View Product',
'search_items' => 'Search Products',
'not_found' => 'No Products Found',
'not_found_in_trash' => 'No Products found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Products',
);
// register post type
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'product'),
'query_var' => true,
'menu_icon' => 'dashicons-randomize',
'supports' => array(
'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'thumbnail',
'author',
'page-attributes'
)
);
register_post_type( 'product', $args );
// register taxonomy
register_taxonomy('product_category', 'product', array('hierarchical' => true, 'label' => 'Category', 'query_var' => true, 'rewrite' => array( 'slug' => 'product-category' )));
}
add_action( 'init', 'product_init' );
register_post_type() function:
<?php register_post_type( $post_type, $args ); ?>
register_post_type()
function accepts two parameters, $post_type
and $args
.
register_taxonomy() function:
<?php register_taxonomy( $taxonomy, $object_type, $args ); ?>
register_taxonomy()
function accepts three parameters, $taxonomy
, $object_type
, and $args
.
register_post_type
and register_taxonomy
both function accept the following arguments in $args
parameter.
After inserting our example code in function.php
file of your current WordPress theme, you’ll see the Products menu appears on the left side menu panel in WordPress admin panel. This custom post type menu allows you to view, add, edit, delete separate contents for the custom post type. Also, you would able to add and manage custom post type categories.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Great i really needed this code to get rid of extra plugins. It work
But there is no filter available how to add filter category
thank you for this great code. It is working so great.
how to add tags along with the category for the custom post type
I followed this exactly and it created the custom post twice in the left menu.
I found code on many other website and they are generating problem now it is solved
bro u made a really good job now i clearly understand how to develop custom post type. Thank you so much