create custom post type in wordpress

create custom post type in wordpress

Do you want to learn to create custom post type in wordpress? There are 2 methods for that one with plugin and other is without plugin. Both methods are easy to use, but some require more work than others. Choose whichever method you prefer.

Before that, let's discuss what is post type?

What is post type in wordpress?

Custom post types are content types similar to posts and pages in wordpress. A post type can be anything like products in woocommerce, Products is also custom type and let's say we can create testinomials as custom post type.

By default, WordPress comes with these post types:

  • Post
  • Page
  • Attachment
  • Nav Menu

Here, in this example we will create custom post type with name 'courses'

The two method are as follow:-

1. Create custom post type with the help of plugin.

2. Create custom post type without any plugin.

Let's talk about first method create custom post type with the help of plugin

Create custom post type with the help of plugin

There are some plugins available from those you can create custom post type

Custom Post Type UI

It's a simple and plug & play plugin and you don't need any coding knowledge to create a custom post type from this plugin. Once you install and activate the plugin. You can go to menu under CPT UI and click on Add/Edit post type.

here is the attach screenshot

Click on Add/Edit post type

once you click on it  a form will appear

As we are creating post type courses, type courses in post type slug, in plural label type courses too and for singular type course. Your custom post type has been created. You can create many custom post type with this plugin. Click here to download this plugin
There are many other plugins that can do the same type of job, but I'm adding this one because it is very simple and easy to use.

Create custom post type without any plugin.

To create custom post type manually here is the code you can just copy paste the code in your theme function.php

function courses_post_type() {
$labels = array(
'name' => _x( 'Course', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Course', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Courses', 'twentythirteen' ),
'all_items' => __( 'All Courses', 'twentythirteen' ),
'view_item' => __( 'View Course', 'twentythirteen' ),
'add_new_item' => __( 'Add New Course', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit Course', 'twentythirteen' ),
'update_item' => __( 'Update Course', 'twentythirteen' ),
'search_items' => __( 'Search Course', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
);

$args = array(
'label' => __( 'Courses', 'twentythirteen' ),
'description' => __( 'Courses review', 'twentythirteen' ),
'labels' => $labels,
// In SUpport you can add or remove what you want in your custom post type, like you want editor, feature image and different suppor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_icon' =>'dashicons-open-folder',
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,

// This is where we add taxonomies to our CPT
'taxonomies' => array( 'category' ),
);

// Registering your Custom Post Type
register_post_type( 'courses', $args );

}

add_action( 'init', 'courses_post_type', 0 );

Here I've created a function called courses_post_type I've put the label array and $args array in register_post_type which is an in-built wordpress function to register post type and $args is the parameter we pass in that function. and by calling add_action I've call my function courses_post_type which register the custom post type to wordpress admin.

Conclusion

I don't support any plugin to create custom post type, You should use minimum WordPress plugins in your site, therefore by some code you can save a plugin to install in your site.