Here, you will learn to create a custom post type without using a plugin in WordPress.
What is Custom Post Type?
A custom post type is a new post type that is customizable according to your preferences. When custom post type is created, it is stored at table (wp_posts) in database.
By default, there are five post types which are already available for the user when you install.
Post
Page
Attachment
Revision
Navigation Menus
Creating a custom post type without using a plugin
Let’s create a Custom Post Type called ‘Product’ using the following given code. First and foremost, add the given function in your “functions.php” file which is located inside your theme folder. Make sure the code is placed before the closing of PHP tag (?>), otherwise it won’t work.
// Product Custom Post type function products_post() { //Set up product labels $labels = array( 'name' => _x( 'Products ', 'post type general name' ), 'singular_name' => _x( 'Product ', 'post type singular name' ), 'add_new' => _x( 'Add New', 'product' ), 'add_new_item' => __( 'Add New Products ' ), 'edit_item' => __( 'Edit Product' ), 'new_item' => __( 'New Products' ), 'all_items' => __( 'All Products' ), 'view_item' => __( 'View Product' ), 'search_items' => __( 'Search Products' ), 'not_found' => __( 'No product found' ), 'not_found_in_trash' => __( 'No product found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Products' ); //register post type $args = array( 'labels' => $labels, 'description' => 'Holds products and product specific data', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 'has_archive' => true, ); register_post_type( 'products', $args ); } add_action( 'init', 'custom_post_event' );
The register_post_type()
function is used to create a post type
<?php register_post_type( $post_type, $args ); ?>
register_post_type() function accepts two parameters, $post_type and $args.
- $post_type – Specify your custom post type.
- $args – An array of the arguments.
To create “custom post categories”. Add the following function in function.php
// Product Custom Post type function products_post() { //Set up product labels $labels = array( 'name' => _x( 'Products ', 'post type general name' ), 'singular_name' => _x( 'Product ', 'post type singular name' ), 'add_new' => _x( 'Add New', 'product' ), 'add_new_item' => __( 'Add New Products ' ), 'edit_item' => __( 'Edit Product' ), 'new_item' => __( 'New Products' ), 'all_items' => __( 'All Products' ), 'view_item' => __( 'View Product' ), 'search_items' => __( 'Search Products' ), 'not_found' => __( 'No product found' ), 'not_found_in_trash' => __( 'No product found in the Trash' ), 'parent_item_colon' => '', 'menu_name' => 'Products' ); //register post type $args = array( 'labels' => $labels, 'description' => 'Holds products and product specific data', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 'has_archive' => true, ); register_post_type( 'products', $args ); // register taxonomy register_taxonomy('product_category', 'product', array('hierarchical' => true, 'label' => 'Category', 'query_var' => true, 'rewrite' => array( 'slug' => 'product-category' ))); } add_action( 'init', 'custom_post_event' );
The register_taxonomy()
function is used to create/register categories for Custom Post Types.
<?php register_taxonomy( $taxonomy, $object_type, $args ); ?>
register_taxonomy() function accepts three parameters, $taxonomy, $object_type, and $args.
- $taxonomy – The Name of the taxonomy.
- $object_type – The name of the object type for the taxonomy object.
- $args – An array of the arguments.
Save the file and go to WordPress Dashboard section and refresh it. Here, you can see custom post type “Products” has been added which will allow to add, edit, delete posts for your CPT “
Conclusion
Custom post type feature is one of the greatest feature in the WordPress. By adding a simple line of code in the function.php you can create and use custom post type