How to create custom post type in wordpress

20 Jul

How Create Custom Content Type post type In WordPress by ultrainfotek

Now WordPress provides very nice feature of creating custom post type but an only WordPress expert can build any new custom post type in the system. WordPress does not provide any user interface for this feature in the admin. Custom post can be built via code only and users can write code in his own plugin or he can write code in the function.php file in the current theme but writing code in the theme has one drawback and that is, the code will work only until the theme is active where you have written the code when you deactivate that theme code will not work so it is better to write code in your own plugin.

Now we will learn how to build a new custom post in the WordPress

WordPress provides action and filters. Action are the events that executes when some action is done in the WordPress like when WordPress initialization, when we publish a post or when we display page so there are many actions available in the WordPress and filters are provided to user to filter or change the data before go for the view or for store in the database.

For creating new custom post type we need to tell WordPress at the time time of initialization that we want new custom post type in the system.

So we need to use Init action of WordPress this action is executed at the time of initialization

add_action( 'init', 'news_post' );

Above code has two arguments one is the action name and second is our function name that should be unique in the system. Now we need to define new_post function and WordPress executed its code at the time of initialization.

function news_post() {
	register_post_type( 'news',
			'labels' => array(
				'name' => __( 'News' ),
				'singular_name' => __( 'News' )
			'public' => true,
			'has_archive' => true,
			'rewrite' => array('slug' => 'newss'),

We have used the register_post_type function in the news_post functions to tell the Wordprss that we want new post type named news. register_post_type function is the main function which is used to register new post type in the WordPress. It accepts various arguments for new post types.

Following is the list of arguments that register_post_type function accepts.


  • ‘name’ – name of post should be plural.
  • ‘singular_name’ – name of post can be same as name.
  • ‘menu_name’ – can be same as name , this is used to display in the menu.
  • ‘name_admin_bar’ – name given for the “Add New” dropdown on admin bar. Defaults to ‘singular_name’ if it exists, ‘name’ otherwise.
  • ‘all_items’ – sting to be used when at all items links.
  • ‘add_new’ – sting to be used at the add new link.
  • ‘add_new_item’ – the add new item text. Default is Add New Post/Add New Page.
  • ‘edit_item’ – the edit item text. In the UI, this label is used as the main header on the post’s editing panel. Default is “Edit Post” for non-hierarchical and “Edit Page” for hierarchical post types.
  • ‘new_item’ – the new item text. Default is “New Post” for non-hierarchical and “New Page” for hierarchical post types.
  • ‘view_item’ – the view item text. Default is View Post/View Page.
  • ‘search_items’ – the search items text. Default is Search Posts/Search Pages.
  • ‘not_found’ – the not found text. Default is No posts found/No pages found.
  • ‘not_found_in_trash’ – the not found in trash text. Default is No posts found in Trash/No pages found in Trash.
  • ‘parent_item_colon’ – the parent text. This string is used only in hierarchical post types. Default is “Parent Page”.
  • description
  • public
  • exclude_from_search
  • publicly_queryable
  • show_ui
  • show_in_nav_menus
  • show_in_menu
  • show_in_admin_bar
  • menu_position
  • menu_icon
  • capability_type
  • capabilities
  • edit_posts
  • edit_others_posts
  • publish_posts
  • read_private_posts
  • read
  • delete_posts
  • delete_private_posts
  • delete_published_posts
  • delete_others_posts
  • edit_private_posts
  • edit_published_posts
  • map_meta_cap
  • hierarchical
  • supports
  • register_meta_box_cb
  • taxonomies
  • has_archive
  • permalink_epmask
  • rewrite
  • query_var
  • can_export
  • _builtin
  • _edit_link
add_action( 'init', 'codex_book_init' );

function codex_book_init() {
	$labels = array(
		'name'               => _x( 'Articles', 'post type general name', 'your-plugin-textdomain' ),
		'singular_name'      => _x( 'Article', 'post type singular name', 'your-plugin-textdomain' ),
		'menu_name'          => _x( 'Articles', 'admin menu', 'your-plugin-textdomain' ),
		'name_admin_bar'     => _x( 'Article', 'add new on admin bar', 'your-plugin-textdomain' ),
		'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
		'add_new_item'       => __( 'Add New Article', 'your-plugin-textdomain' ),
		'new_item'           => __( 'New Article', 'your-plugin-textdomain' ),
		'edit_item'          => __( 'Edit Article', 'your-plugin-textdomain' ),
		'view_item'          => __( 'View Article', 'your-plugin-textdomain' ),
		'all_items'          => __( 'All Articles', 'your-plugin-textdomain' ),
		'search_items'       => __( 'Search Articles', 'your-plugin-textdomain' ),
		'parent_item_colon'  => __( 'Parent Articles:', 'your-plugin-textdomain' ),
		'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
		'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )

	$args = array(
		'labels'             => $labels,
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )

	register_post_type( 'book', $args );

click here to read more about custom post type.

If you find creating custom type tough and you \ want to save your precious time, we recommend Simple Custom post type custom field plugin for you. This is a very simple plugin and you can build new custom post in just two clicks.

It has many Rich Features like Create Custom post types, Add many types of fields (like date, content ref, user ref.), Add fields in a list view, Add metainfo (SEO).

You can download the WordPress plugin from the download link.

Leave a Reply

Your email address will not be published. Required fields are marked *