Last updated on September 13, 2023

Add Custom Post Type

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Register a custom post type.

Before you start

WordPress allows you to create custom post types, which are different from the default “post” and “page” post types. Custom post types allow you to create custom content types with their own set of fields and taxonomies. The main function to use in order to register a post type, is register_post_type().

Registers a post type

Parameters

$post_type string
(Required)

The post type slug. Must not exceed 20 characters and may only contain lowercase alphanumeric characters, dashes, and underscores.

$args array|string
(Optional)

An array of arguments for registering the post type

Return

WP_Post_Type|WP_Error

The registered post type object on success, WP_Error object on failure.

Always make sure to call register_post_type() within a function that is hooked to the init hook. For example:

add_action( 'init', 'my_register_cpt' );

function my_register_cpt() {

    register_post_type( 'location', array(
       ...
    ) );
}

It is a good practice to register your custom post types within a plugin instead of a theme. This guarantees that any user-generated content will not be lost, even if another theme is activated.

How to register a custom post type without a plugin?

function wpsnippets_register_cpt_location() {

	register_post_type( 'location',
		array(
			'labels'      => array(
	 	     'name'           => _x( 'Locations', 'Post type general name', 'wpsnippets' ),
		     'singular_name'  => _x( 'Location', 'Post type singular name', 'wpsnippets' ),
			),
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array( 'slug' => 'location' ),
		)
	);
}

add_action('init', 'wpsnippets_register_cpt_location');

This code defines a custom post type called “Location” with a slug of location. You can modify the name, slug, and other parameters to suit your needs. A more extensive example:

/**
 * Register a custom post type called "location".
 *
 * @see get_post_type_labels() for label keys.
 */
function wpsnippets_register_cpt_location() {

	$labels = array(
		'name'                  => _x( 'Locations', 'Post type general name', 'wpsnippets' ),
		'singular_name'         => _x( 'Location', 'Post type singular name', 'wpsnippets' ),
		'menu_name'             => _x( 'Locations', 'Admin Menu text', 'wpsnippets' ),
		'name_admin_bar'        => _x( 'Location', 'Add New on Toolbar', 'wpsnippets' ),
		'add_new'               => __( 'Add New', 'wpsnippets' ),
		'add_new_item'          => __( 'Add New Location', 'wpsnippets' ),
		'new_item'              => __( 'New Location', 'wpsnippets' ),
		'edit_item'             => __( 'Edit Location', 'wpsnippets' ),
		'view_item'             => __( 'View Location', 'wpsnippets' ),
		'all_items'             => __( 'All Locations', 'wpsnippets' ),
		'search_items'          => __( 'Search Locations', 'wpsnippets' ),
		'parent_item_colon'     => __( 'Parent Locations:', 'wpsnippets' ),
		'not_found'             => __( 'No Locations found.', 'wpsnippets' ),
		'not_found_in_trash'    => __( 'No Locations found in Trash.', 'wpsnippets' ),
		'featured_image'        => _x( 'Location thumbnail', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'wpsnippets' ),
		'set_featured_image'    => _x( 'Set thumbnail', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'wpsnippets' ),
		'remove_featured_image' => _x( 'Remove thumbnail', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'wpsnippets' ),
		'use_featured_image'    => _x( 'Use as thumbnail', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'wpsnippets' ),
		'archives'              => _x( 'Location archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'wpsnippets' ),
		'insert_into_item'      => _x( 'Insert into Location', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'wpsnippets' ),
		'uploaded_to_this_item' => _x( 'Uploaded to this Location', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'wpsnippets' ),
		'filter_items_list'     => _x( 'Filter Locations list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'wpsnippets' ),
		'items_list_navigation' => _x( 'Locations list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'wpsnippets' ),
		'items_list'            => _x( 'Locations list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'wpsnippets' ),
	);

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

	register_post_type( 'location', $args );
}

add_action( 'init', 'wpsnippets_register_cpt_location' );

Once you have added this code snippet, the custom post type will be available in the WordPress admin menu, and you can create, edit, and delete any posts of your custom post type just like regular posts and pages. The supports parameter lists the features you want to enable for your custom post type, such as the title, editor, excerpt, thumbnail, comments, etc.

Last updated on September 13, 2023. Originally posted on April 30, 2023.

Leave a Reply

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