How to Create Custom Post Type (CPT) in WordPress Without Plugin

Image Not Found

In this article, I’ll show you how you can create a custom post type (CPT) in WordPress with snippets.

Pest this code on your function.php file.

/**
 * Creating WordPress Custom Post Type (CPT)
 */
add_action('init', function() {

	$labels = array(
		'name'              => _x('Reviews', 'plural'),
		'singular_name'     => _x('Reviews', 'singular'),
		'menu_name'         => _x('Reviews', 'admin menu'),
		'name_admin_bar'    => _x('Reviews', 'admin bar'),
		'add_new'           => _x('Add Reviews', 'add new'),
		'add_new_item'      => __('Add New Reviews'),
		'new_item'          => __('New Reviews'),
		'edit_item'         => __('Edit Reviews'),
		'view_item'         => __('View Reviews'),
		'all_items'         => __('All Reviews'),
		'search_items'      => __('Search Reviews'),
		'not_found'         => __('No Reviews found.')
	);
	$supports = array('title', 'editor', 'author', 'thumbnail');
	$taxonomies = array();
	$rewrite = array('slug' => 'reviews');

	$args = array(
		'labels'                => $labels,
		'supports'              => $supports,
		'description'           => 'This is Reviews panel',
		'public'                => true,
		'taxonomies'            => $taxonomies,
		'show_ui'               => true,
		'show_in_menu'          => true,
		'show_in_nav_menus'     => true,
		'show_in_admin_bar'     => true,
		'can_export'            => true,
		'capability_type'       => 'post',
		'show_in_rest'          => true,
		'query_var'             => true,
		'rewrite'               => $rewrite,
		'has_archive'           => true,
		'hierarchical'          => true,
		'menu_position'         => 22,
		'menu_icon'             => 'dashicons-book'
	);

	register_post_type('reviews', $args); // Register Post type
});

That’s all. I hope this article helped you understand How you can create CPT with snippets.

Picture of SIFAT ULLAH

SIFAT ULLAH

Growing knowledge in both front-end and back-end development. With a solid foundation in JavaScript, TypeScript, Node.js, React.js, Express.js, MongoDB, Mongoose, Prisma, and PostgreSQL.

Join My Newsletter