If you are a wordpress developer then you must need to read this article. In this article i will tell you how to create custom post type in wordpress without using plugin . As you know wordpress has different post types and a single page of content is called a post. Also every post is a specific post type like post, page, attachment etc. All the posts content are stored in same database table named (“wp_posts” ) and post content is differentiated by table column called “post_type”.
By default , after installation of wordpress you will get different post types which is listed below:
- Post (Post Type: ‘post’)
- Page (Post Type: ‘page’)
- Attachment (Post Type: ‘attachment’)
- Revision (Post Type: ‘revision’)
- Navigation menu (Post Type: ‘nav_menu_item’)
- Custom CSS (Post Type: ‘custom_css’)
- Changesets (Post Type: ‘customize_changeset’)
- User Data Request (Post Type: ‘user_request’ )
Now if you want to create custom post type in wordpress without using plugin then it is very easy and you don’t need to use any plugin. We can create a custom post type in wordpress using register_post_type() function. This custom post type will have its own category and custom field.
In this article we will create a custom post type : ‘job_listing’. This custom post type will help you to add job listing content in wordpress. For example: if your website offer some job opening content to your users and you want to separate the job listing section from the posts or page section. In that case, you would probably need a separate job listing post type. This custom post type will have its own custom category and custom fields.
Create Custom Post Type
At the time of adding a post type, you always need to register taxonomies of that post type. You can define and register a taxonomy using register_taxonomy()
function. The register_post_type()
function is used to create a post type and it only be called through the init
action hook.
You only need to open and modify functions.php file which is located in /wp-content/themes/theme_name/functions.php. So open the functions.php file and copy and paste following code:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// Job Listing Custom Post Type function job_init() { // set up Job Listing labels $labels = array( 'name' => 'Job Listings', 'singular_name' => 'Job Listing', 'add_new' => 'Add New Job Listing', 'add_new_item' => 'Add New Job Listing', 'edit_item' => 'Edit Job Listing', 'new_item' => 'New Job Listing', 'all_items' => 'All Job Listing', 'view_item' => 'View Job Listing', 'search_items' => 'Search Job Listings', 'not_found' => 'No Job Listings Found', 'not_found_in_trash' => 'No Job Listings found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Job Listings', ); // register post type $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'job_listing'), 'query_var' => true, 'menu_icon' => 'dashicons-randomize', 'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes' ) ); register_post_type( 'job_listing', $args ); // register taxonomy register_taxonomy('job_category', 'job_listing', array('hierarchical' => true, 'label' => 'Category', 'query_var' => true, 'rewrite' => array( 'slug' => 'job-category' ))); } add_action( 'init', 'job_init' ); |
2 3 4 |
<?php register_post_type( $post_type, $args ); ?> |
You can see register_post_type()
function accepts two parameters, $post_type
and $args
.
- $post_type – Specify your custom post type.
- $args – An array of the arguments.
2 3 4 |
<?php register_taxonomy( $taxonomy, $object_type, $args ); ?> |
You can see 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 arguments in $args
parameter. you can check following link for arguments more details:
https://codex.wordpress.org/Function_Reference/register_post_type
After inserting above example code in function.php
file, you can see the Job Listing menu appears on the left side menu in WordPress admin panel. This custom post type menu allows you to view, add, edit, delete separate contents for the custom post type and manage custom post type categories.
SEE:How to Show Related Posts for Custom Post Type in WordPress
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co