How to create custom post type in WordPress

Custom Post types are content types which can store different types of contents. By default, WordPress comes with five built in post types,

  1. Post (Post Type: ‘post’)
  2. Page (Post Type: ‘page’)
  3. Attachment (Post Type: ‘attachment’)
  4. Revision (Post Type: ‘revision’)
  5. Navigation menu (Post Type: ‘nav_menu_item’)

You can add new custom post types to WordPress using the register_post_type() function.  It accepts two parameters, first a unique name for the custom post type and second is an array of arguments which sets the options for the custom post types.

Useful Read: How To Add Custom Post Types To At A Glance Dashboard Widget In WordPress

In this tutorial, I am going to create a post type called Book. For this tutorial, I am going to put all the code in functions.php,  but you can also create your own custom plugin if you want.

To create a custom post type, paste the following to your functions.php. 

add_action('init','ccw_book_init');
function ccw_book_init() {
	$labels = array(
		'name'               => _x( 'Books', 'post type general name' ),
		'singular_name'      => _x( 'Book', 'post type singular name' ),
		'menu_name'          => __( 'Books' ),
		'add_new'            => __( 'Add New' ),
		'add_new_item'       => __( 'Add New Book' ),
		'new_item'           => __( 'New Book' ),
		'edit_item'          => __( 'Edit Book' ),
		'view_item'          => __( 'View Book' ),
		'all_items'          => __( 'All Books' ),
		'search_items'       => __( 'Search Books' ),
		'parent_item_colon'  => __( 'Parent Books:' ),
		'not_found'          => __( 'No books found.' ),
		'not_found_in_trash' => __( 'No books found in Trash.' )
	);

	$args = array(
		'labels'             => $labels,
		'public'             => true,
		'show_ui'            => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'has_archive'        => true,
		'menu_position'      => 5,
		'supports'           => array( 'title', 'editor', 'thumbnail')
	);

	register_post_type( 'book', $args );
}

As you can see, we have added various arguments to the custom post types with this code. So, let’s run over those arguments,

labels – An array of labels for the new custom post type.

public – Controls the visibility of your post type for both front end and back end.

show_ui – Determines whether to display a UI on the dashboard for managing custom post type.

rewrite – An array of arguments which allows you to set slug and permalink structure for the custom post type.

has_archive – Enables post type archives.

menu_position – Determines the menu position for the custom post type on the dashboard.

supports – An array of arguments which determines the features that are available on edit screen of the custom post type.

Once, you have added all the code to your functions.php file, go back to your dashboard and refresh the screen, you should be able to see the custom post type on the left panel of the dashboard.

In the next tutorial, we will take this to the next level and add a custom taxonomy to this custom post type. In case you have any queries feel free to comment.

2 Comments

  1. can you help me displaying the custom fields this is my code
    ‘movie’,

    );
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {

    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    ?>

    <a href="”>

    Reply

Leave a Comment.