Create a custom post type

Creating a custom post type on WordPress can be a valuable way to organize your website’s content and improve its functionality. Custom post types allow you to create unique templates for specific types of content, such as events, products, or testimonials. In this article, we’ll go through the steps you need to follow to create a custom post type on WordPress.

Step 1: Choose a Name and Slug

The first step in creating a custom post type is to choose a name and slug for it. The name should be descriptive and easy to understand, while the slug should be a unique identifier for the post type. For example, if you’re creating a custom post type for events, you might call it “Events” and use the slug “event”.

Step 2: Define the Post Type

The next step is to define the post type in your WordPress code. You can do this by adding a new function to your theme’s functions.php file. Here’s an example:

function wp_daily_event_post_type() {
	$labels = array(
		'name'                  => _x( 'Events', 'Post type general name', 'event' ),
		'singular_name'         => _x( 'Event', 'Post type singular name', 'event' ),
		'menu_name'             => _x( 'Events', 'Admin Menu text', 'event' ),
		'name_admin_bar'        => _x( 'Event', 'Add New on Toolbar', 'event' ),
		'add_new'               => __( 'Add New', 'event' ),
		'add_new_item'          => __( 'Add New event', 'event' ),
		'new_item'              => __( 'New event', 'event' ),
		'edit_item'             => __( 'Edit event', 'event' ),
		'view_item'             => __( 'View event', 'event' ),
		'all_items'             => __( 'All events', 'event' ),
		'search_items'          => __( 'Search events', 'event' ),
		'parent_item_colon'     => __( 'Parent events:', 'event' ),
		'not_found'             => __( 'No events found.', 'event' ),
		'not_found_in_trash'    => __( 'No events found in Trash.', 'event' ),
		'featured_image'        => _x( 'Event Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'event' ),
		'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'event' ),
		'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'event' ),
		'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'event' ),
		'archives'              => _x( 'Event archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'event' ),
		'insert_into_item'      => _x( 'Insert into event', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'event' ),
		'uploaded_to_this_item' => _x( 'Uploaded to this event', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'event' ),
		'filter_items_list'     => _x( 'Filter events 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', 'event' ),
		'items_list_navigation' => _x( 'Events 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', 'event' ),
		'items_list'            => _x( 'Events list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'event' ),
	);
	$args = array(
		'labels'             => $labels,
		'description'        => 'Event custom post type.',
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'event' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => 20,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail' ),
		'taxonomies'         => array(),
		'show_in_rest'       => true
	);
	
	register_post_type( 'event', $args );
}
add_action('init', 'wp_daily_event_post_type');

In this example, we’re defining a custom post type called “event” with two labels: “Events” (for the plural) and “Event” (for the singular). We’ve set the post type to be public (meaning it can be accessed by anyone) and enabled archives (so you can display all events on a single page). The register_post_type() function accepts an array of parameters that define various aspects of the custom post type. Some of the parameters that can be defined include the labels for the post type, the public status, the menu icon, the taxonomy, and more.

Step 3: Add Custom Fields

Custom post types can be even more powerful when you add custom fields to them. Custom fields allow you to add additional information to your posts that wouldn’t normally be available. You can do this by using a plugin such as Advanced Custom Fields, or by creating your own custom fields using WordPress code.

function wp_daily_event_meta_box() {
	add_meta_box(
		'event_meta_box',
		__( 'Event Details' ),
		'wp_daily_event_meta_box_callback',
		'event',
		'normal',
		'default'
	);
}
add_action( 'add_meta_boxes', 'wp_daily_event_meta_box' );

function wp_daily_event_meta_box_callback( $post ) {
	wp_nonce_field( 'event_meta_box', 'event_meta_box_nonce' );
	$event_location = get_post_meta( $post->ID, '_event_location', true );
	$event_date = get_post_meta( $post->ID, '_event_date', true );
	?>
	<label for="event_location"><?php _e( 'Location' ); ?></label>
	<input type="text" id="event_location" name="event_location" value="<?php echo esc_attr( $event_location ); ?>">

	<br>

	<label for="event_date"><?php _e( 'Date' ); ?></label>
	<input type="date" id="event_date" name="event_date" value="<?php echo esc_attr( $event_date ); ?>">
	<?php
}

function save_event_meta_box_data( $post_id ) {
	if ( ! isset( $_POST['event_meta_box_nonce'] ) ) {
		return;
	}
	
	if ( ! wp_verify_nonce( $_POST['event_meta_box_nonce'], 'event_meta_box' ) ) {
		return;
	}
	
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}
	
	$event_location = sanitize_text_field( $_POST['event_location'] );
	$event_date = sanitize_text_field( $_POST['event_date'] );
	
	update_post_meta( $post_id, '_event_location', $event_location );
	update_post_meta( $post_id, '_event_date', $event_date );
}
add_action( 'save_post', 'save_event_meta_box_data' );

Step 4: Display Your Custom Post Type

Once you’ve created your custom post type, you’ll want to display it on your website. You can do this by creating a new template file in your theme’s folder. For example, if you’ve created a custom post type for events, you might create a file called “single-event.php”. This file should contain the code that you want to display for each event post.

Step 5: Customize Your Custom Post Type

Finally, you can customize your custom post type further by adding additional features or functionality. For example, you might want to add a custom taxonomy (a way of categorizing your posts) or custom post meta (additional information that’s specific to each post).

In conclusion, creating a custom post type on WordPress can be a great way to organize your website’s content and improve its functionality. By following the steps outlined in this article, you can create a custom post type that’s tailored to your website’s needs. Remember to choose a descriptive name and slug, define the post type in your WordPress code, add custom fields, display your custom post type, and customize it further to meet your needs.

Leave a Comment

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