How to Create Custom Post Types in WordPress without using Plugin

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.

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)

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.

Create Custom Post Type

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(
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.

  • $post_type – Specify your custom post type.
  • $args – An array of the arguments.

register_taxonomy() function:

<?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.

register_post_type and register_taxonomy both function accept the following arguments in $args parameter.

  • label – A plural descriptive name for the post type.
  • labels – An array of labels for the post type.
  • description – A short description of the post type.
  • public – Visibility control for authors and readers.
  • show_ui – Whether generates a default UI for this post type in the admin panel.
  • show_in_nav_menus – Whether the post type is available in the navigation menu.
  • show_in_menu – Whether the post type would appear in the admin menu.
  • show_in_admin_bar – Whether the post type available in the admin bar.
  • menu_position – The position of the post type in menu order.
  • menu_icon – Specify the menu icon.
  • capability_type – The string is used to build the read, edit, and delete capabilities.
  • capabilities – An array of the capabilities for the post type.
  • map_meta_cap – Whether the internal default meta capability handling is used.
  • hierarchical – Whether the post type is hierarchical.
  • supports – An alias for calling add_post_type_support() directly.
  • register_meta_box_cb – Provide a callback function that will be called when setting up the meta boxes for the edit form.
  • taxonomies – An array of registered taxonomies.
  • has_archive – Enables the post type archives.
  • permalink_epmask – The default rewrite endpoint bitmasks.
  • rewrite – Triggers the handling of rewrites for the post type.
  • query_var – Sets the query_var for the post type.
  • can_export – Can the post type be exported.
  • show_in_rest – Whether the post type is expose in REST API.
  • rest_base – The base slug while accessed using the REST API.
  • rest_controller_class – An optional custom controller to use instead of WP_REST_Posts_Controller.
  • _builtin – Whether the post type is native or “built-in”.
  • _edit_link – Link to edit an entry with the post.

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.


Related Articles

Comments 0