Create Custom Post Types

How to Create Custom Post Types in WordPress (3 ways)

You are using the most popular Content Management system. WordPress builds on post type. But You want to know How to Create Custom Post Types in WordPress. We will show 3 different ways to create custom post types.

In this article, You will learn all possible ways to create a custom post type.  You can choose which is best for you.

What is Custom Post Types:

WordPress is full of content. It has different types of content. Each type of content has a unique name. But overall types of content name as Post type.

WordPress has additional default post types. There are five post types users can see. 

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)

WordPress offers to create custom post types. You can create your own content type. Theme developer can create custom post types as post or page.

Create Custom Post types Without plugin (Manually):

First way to create a custom post type. Now we create custom post types totally manually. If you want to create your own content type. Most common custom post type is Team, Portfolio and Testimonial. And You can create for additional uses. 

We use WordPress default function register_post_type(). If you work as a theme developer. You need to know the full feature of the register_post_type() function. For example, we create custom post type names as “team”.

Look the algorithm:

if ( ! function_exists('wpk_team_post_type') ):

// Register Custom Post Type
function wpk_team_post_type() {

	$labels = array(
		'name'                  => _x( 'Teams', 'Post Type General Name', 'wpknifer' ),
		'singular_name'         => _x( 'Team', 'Post Type Singular Name', 'wpknifer' ),
		'menu_name'             => __( 'Team', 'wpknifer' ),
		'name_admin_bar'        => __( 'Team', 'wpknifer' ),
		'archives'              => __( 'Item Archives', 'wpknifer' ),
		'attributes'            => __( 'Item Attributes', 'wpknifer' ),
		'parent_item_colon'     => __( 'Parent Item:', 'wpknifer' ),
		'all_items'             => __( 'All Items', 'wpknifer' ),
		'add_new_item'          => __( 'Add New Item', 'wpknifer' ),
		'add_new'               => __( 'Add New', 'wpknifer' ),
		'new_item'              => __( 'New Item', 'wpknifer' ),
		'edit_item'             => __( 'Edit Item', 'wpknifer' ),
		'update_item'           => __( 'Update Item', 'wpknifer' ),
		'view_item'             => __( 'View Item', 'wpknifer' ),
		'view_items'            => __( 'View Items', 'wpknifer' ),
		'search_items'          => __( 'Search Item', 'wpknifer' ),
		'not_found'             => __( 'Not found', 'wpknifer' ),
		'not_found_in_trash'    => __( 'Not found in Trash', 'wpknifer' ),
		'featured_image'        => __( 'Featured Image', 'wpknifer' ),
		'set_featured_image'    => __( 'Set featured image', 'wpknifer' ),
		'remove_featured_image' => __( 'Remove featured image', 'wpknifer' ),
		'use_featured_image'    => __( 'Use as featured image', 'wpknifer' ),
		'insert_into_item'      => __( 'Insert into item', 'wpknifer' ),
		'uploaded_to_this_item' => __( 'Uploaded to this item', 'wpknifer' ),
		'items_list'            => __( 'Items list', 'wpknifer' ),
		'items_list_navigation' => __( 'Items list navigation', 'wpknifer' ),
		'filter_items_list'     => __( 'Filter items list', 'wpknifer' ),
	);
	$args = array(
		'label'                 => __( 'Team', 'wpknifer' ),
		'description'           => __( 'Team Description', 'wpknifer' ),
		'labels'                => $labels,
		'supports'              => array( 'title', 'editor', 'thumbnail' ),
		'hierarchical'          => false,
		'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'           => true,
		'exclude_from_search'   => false,
		'publicly_queryable'    => true,
		'capability_type'       => 'post',
	);
	register_post_type( 'team', $args );

}
add_action( 'init', 'wpk_team_post_type', 0 );
endif;

Uses: If you copy the algorithm and paste on your function.php. A custom post type was created named “Team”. You can create it with your own name.

Argument  Description:  We are going to describe argument uses. So, You use it as a smart user. 

  • Label: The name is shown in the menu.
  • Description: A summary of the post type.
  • Labels: All Labels for the name.
  • Supports: Core features include ‘title’, ‘editor’, ‘comments’, ‘revisions’, ‘trackbacks’, ‘author’, ‘excerpt’, ‘page-attributes’, ‘thumbnail’, ‘custom-fields’, and ‘post-formats’.
  • Public: Do you want to show publicly?
  • Show_ui: Do you want to show the URL?
  • Show_in_menu: Do you want to show the name in the menu?
  • Menu_position: What number of positions in the admin menu.
  • Show_in_admin_bar: Do you want to show in the admin bar menu?
  • Show_in_nav_menus: Do you want an option in nav menus?

We show you the important argument that can be used easily. If You think it is not enough. You can visit register_post_type.

Use Generator Tools for Create Custom Post types:

Some people love to use generator tools. You will get every single option visually. If you don’t understand That algorithm What to do? That time you can use generator tools. 

GenerateWP: One of the best tools that I love. You can easily generate custom post types, taxonomy, and lots of features. To generate custom post type follow the step:

  1. Go to the link and click the tab named “General” filled with the function name and other needs.
  2. Click the other tabs filled with your needs.
  3. Then click the button name “Updated Code”
  4. The code will be generated below.
  5. Copy the code and paste it into your function.php file.

This is the easiest generating tool. I am using it.

WordPress Plugins for Create Custom Post types:

If you think you can use code. We have options for you. You can use WordPress Plugin to create custom post types. But I am not recommending you to use it for creating custom Post types.

Best plugin for create custom posts types:

  1. Custom Post Type UI : Custom Post Type UI provides an easy to use interface for registering and managing custom post types and taxonomies for your website.
  2. Custom Post Type Maker: Custom Post Type Maker is the perfect plugin to create Custom Post Types and custom Taxonomies in a user friendly way, just like managing your regular posts and pages.

Archive Template WordPress for Custom Post types:

Every post type has its own archive page. If you do not create an archive template. The archive page design comes from index.php. So, We need custom design for our custom post types. 

To create a custom post-type archive template. Create a file name archive-{post-type-name}.php. For example, we create a “team” post type. So, we created a file named  archive-team.php in the theme root location. Create A template like index.php. We give you an example. 

get_header();


if(have_posts()):
	while (have_posts()) : the_post();
		 
		 get_template_part('template-parts/content', get_post_type());

	endwhile;
endif;

get_footer();

In this example we use get_template_part() and give location “template-parts/content-team.php”. Now you can create a content-team.php template in the template part folder. 

Single Template for Custom Post types:

Do you want to create a single post template like a WordPress post? Well, We are going to create a single post template for custom posts. According to WordPress, Create a custom post type single post template name single-{Post type}.php. So, we can create a file named single-team.php in the theme root folder.  

Example:

get_header();


if(have_posts()):
	while (have_posts()) : the_post();
		 
		?>
		<div id="single-team-<?php get_the_ID(); ?>" <?php post_class('single-team'); ?>>
			<?php the_content(); ?>
		</div>
		<?php 

	endwhile;
endif;

get_footer();

We are using page builder so we use the_content() function. Now a single post is set up. 

Display Custom Post types With Custom Query:

Sometimes we will need to load a custom post type in a while loop or in another template. That time we can use custom queries. This query will be used anywhere you want.

Remember, You need to check if the register_post_type argument named “publicly_queryable” value is true, Then it will get output. Otherwise, it will not be displayed. 

Example:

$args = array( 
	'post_type' => 'team',
	'post_status' => 'publish',
	'posts_per_page' => 4,
	'ignore_sticky_posts' => true, 
);
$team = new WP_Query($args);

if ($team->have_posts()) {
	while ($team->have_posts()) {
		$team->the_post();

		the_title();
	}
}
wp_reset_query();

 You can use this query.

Conclusion: 

Post type is the most important part of WordPress. I think now you can create a custom post type.  You have to answer each question about it.  And this article has helped you. 

If you have any problem with it. Please comment below. We are ready to help you.

See More:

  1. Display Related Posts in WordPress Without Plugins
  2. How to Display Popular Posts by Views in WordPress Without a Plugin
  3. How To Increase WordPress Website Speed Without Plugin

Leave a Reply

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