If you want to add extra content to your WordPress site beyond the standard posts and pages, you can use custom post types. Custom post types let you add new content types with their own fields and settings.

WordPress comes with a few different post types by default:

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

In this tutorial, we’ll show you how to create custom post type in WordPress.

Create Custom Post Types in WordPress

Creating custom post types is a bit more advanced than creating a regular post or page in WordPress.

To create a custom post type, you’ll need to use a plugin or write some code.

Follow the steps below to create a custom post type on a WordPress website:

  • Navigate to the function.php file from your WordPress theme directory
  • Add the following code to the function.php file
function techzpad_custom_post_type() {
	register_post_type( 'event',
		array(
			'labels' => array(
				'name' => __( 'Events' ),
				'singular_name' => __( 'Event' )
			),
			'public' => true,
			'has_archive' => true,
			'rewrite' => array('slug' => 'event'),
			'show_in_rest' => true,

		)
	);
}
add_action( 'init', 'techzpad_custom_post_type' );

This code snippet registers the post type ‘event’ with an array of arguments. These arguments are the options of our custom post type.

create custom post types in wordpress

When creating custom post types, it is necessary to use init for the hook in add_action(), and the register_post_type() function will take the arguments.

function techzpad_post_type_event() {
	$labels = array(
		'name'                  => _x( 'Event', 'Post Type General Name', 'techzpad' ),
		'singular_name'         => _x( 'Event', 'Post Type Singular Name', 'techzpad' ),
		'menu_name'             => __( 'Event', 'techzpad' ),
		'name_admin_bar'        => __( 'Event', 'techzpad' ),
		'archives'              => __( 'Event Archives', 'techzpad' ),
		'parent_item_colon'     => __( 'Parent Event:', 'techzpad' ),
		'all_items'             => __( 'All Event', 'techzpad' ),
		'add_new_item'          => __( 'Add New Event', 'techzpad' ),
		'add_new'               => __( 'Add Event', 'techzpad' ),
		'new_item'              => __( 'New Event', 'techzpad' ),
		'edit_item'             => __( 'Edit Event', 'techzpad' ),
		'update_item'           => __( 'Update Event', 'techzpad' ),
		'view_item'             => __( 'View Event', 'techzpad' ),
		'search_items'          => __( 'Search Event', 'techzpad' ),
		'not_found'             => __( 'Not found', 'techzpad' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'techzpad' ),
		'featured_image'        => __( 'Featured Image', 'techzpad' ),
		'set_featured_image'    => __( 'Set featured image', 'techzpad' ),
		'remove_featured_image' => __( 'Remove featured image', 'techzpad' ),
		'use_featured_image'    => __( 'Use as featured image', 'techzpad' ),
		'insert_into_item'      => __( 'Insert into item', 'techzpad' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'techzpad' ),
		'items_list'            => __( 'Items list', 'techzpad' ),
		'items_list_navigation' => __( 'Items list navigation', 'techzpad' ),
		'filter_items_list'     => __( 'Filter items list', 'techzpad' ),
	);
	$args = array(
		'label'                 => __( 'Event', 'techzpad' ),
		'description'           => __( 'Descrizione featureo', 'techzpad' ),
		'menu_icon' => 'dashicons-calendar',
		'labels'                => $labels,
		'supports'              => array('title', 'thumbnail', 'editor' ),
		'hierarchical'          => true,
		'public'                => true,
		'show_ui'               => true,
		'show_in_menu'          => true,
		'menu_position'         => 5,
		'show_in_admin_bar'     => true,
		'show_in_nav_menus'     => true,
		'can_export'            => true,
		'has_archive'           => 'event',        
		'exclude_from_search'   => false,
		'publicly_queryable'    => true,
		'capability_type'       => 'page',
		'rewrite' => array(
			'slug' => 'event/post',
			'with_front' => false
		),              
	);
	register_post_type( 'event', $args );

}
add_action( 'init', 'techzpad_post_type_event' );

function techzpad_taxonomy_event() {  
	register_taxonomy(  
		'events_categories',  
		'event',        
		array(  
			'hierarchical' => true,  
			'label' => 'Event Categories',
			'query_var' => true,
			'rewrite' => array(
				'slug' => 'event', 
				'with_front' => false 
			)
		)  
	);  
}  
add_action( 'init', 'techzpad_taxonomy_event' );

As you can see, we have added many more options to the custom post type with this code. It will add more features like support for revisions, featured images, custom fields, and more.

We have also associated this custom post type with a custom taxonomy called ‘genres’.

Also Read : Chane WordPress Login Logo and URL without Plugin

$labels: Specifies that the post type is referred correctly to the admin area.

$args: Specifies a permalink slug of the news and a menu position located just beneath the Posts menu.

Now let’s take a look at before and after adding custom post features to our WordPress website.

Custom Post Type With Category

The steps above tell how to register WordPress custom post types to the backend of any theme.

Querying Custom Post Types

If you are familiar with coding and would like to run loop queries in your templates, then here is how to do that. By querying the database, you can retrieve items from a custom post type.

<?php
$loop = new WP_Query( 
	array( 
		'post_type' => 'event', 
		'showposts' => 5,'
                'post_status' => 'publish',
                'order' => 'DESC'
	)
); 
if ( $loop->have_posts() ) :
	while ( $loop->have_posts() ) : $loop->the_post(); ?>
		<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
		<div class="entry-content">
			<?php the_post_thumbnail('full'); ?>
		</div>
		<?php 
	endwhile;
endif;
wp_reset_postdata();
?>

This code defines the post type and number of posts per page in the arguments for our new WP_Query class. It then runs the query, retrieves the posts, and displays them inside the loop.

Leave Your Comment