How to create custom post type in WordPress

Are you want to create a custom post type in your WordPress site, then this tutorial is for you. In this tutorial, i am going to explain how to create custom post type in WordPress.

This will help you create custom post types. WordPress have some default post types like post and page and WordPress also give you privileges to create unlimited post types as per your requirement. Bellow are the simple steps to create and use custom post types in your WordPress sites.

Default Post Types

Below are the lists of default post types which come up with default WordPress installation:

How to Create a Custom Post Type

Just assume you have a school website and you want to create a custom post type named subjects. Now open your functions.php file from your current activated theme and copy paste below snippet. This will registered a custom post type.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

//register custom post type as subjects

functioncreate_subjectsposttype(){

$args=array(

'public'=>true,

'labels'=>array(

'name'=>__('Subjects'),

'singular_name'=>__('subject')

),

);

register_post_type('custompost_subjects',$args);

}

add_action('init','create_subjectsposttype');

How to Add Custom Post Types in Main Query

If you have created a custom post type, it does not mean that it will automatically added in the main query of WordPress you have to add your custom post type in WordPress main query.
Below is the example of how to add your custom post type in main query. Copy paste

About: Prem Tiwari

Prem Tiwari is the founder of freewebmentor.com and is a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby.
View all posts by Prem Tiwari