How to create your own custom post types in WordPress
Tags: Wordpress Development, Tutorial, WordPress, Custom Post Types

If you have custom, recurring content on your page or blog, e.g. case studies, events, product presentations, reviews, portfolios or testimonials, it is a good idea to create your own custom post types.

The advantages of your own post types: You can tailor your own Custom Post Types to your needs in terms of content and presentation (design).

Of course, there are also plugins for creating post types. Often these plugins are overloaded with useless functions, tracking code, scripts and styles. Overloaded plugins are at the expense of your website performance, security and data protection. If you want to give your visitors a positive, trustworthy user experience, you should create your own post types.

Said and done! We’re creating a new post type

With the help of the WordPress function register_post_type you can now enter your new post type in the functions.php register your active theme. In my example, I'll name the new Custom Post Type Showcase. You are of course free to choose a name that corresponds to your new post type.

Now take the following steps to create and register a new post type:

Log in to the admin area / backend of your WordPress site. Go to Designs -> Theme-Editor, choose your theme and open the file funtions.php. Add the following lines of code in your functions.php

add_action( ‘init’, ‘devslife_showcase_post_type_init’ );

function devslife_showcase_post_type_init() {

// Post type labels, see: https://developer.wordpress.org/reference/functions/get_post_type_labels/

$labels = array(

‘name’ => _x( ‘Showcases’, ‘showcase’ ),

‘singular_name’ => _x( ‘Showcase’, ‘showcase’ ),

‘menu_name’ => _x( ‘Showcases’, ‘showcase’ ),

‘name_admin_bar’ => _x( ‘Showcase’, ‘showcase’ ),

‘add_new’ => __( ‘Add New’, ‘showcase’ ),

‘add_new_item’ => __( ‘Add New Showcase’, ‘showcase’ ),

‘new_item’ => __( ‘New Showcase’, ‘showcase’ ),

‘edit_item’ => __( ‘Edit Showcase’, ‘showcase’ ),

‘view_item’ => __( ‘View Showcase’, ‘showcase’ ),

‘all_items’ => __( ‘All Showcases’, ‘showcase’ ),

‘search_items’ => __( ‘Search Showcases’, ‘showcase’ ),

‘parent_item_colon’ => __( ‘Parent Showcases:’, ‘showcase’ ),

‘not_found’ => __( ‘No Showcases found.’, ‘showcase’ ),

‘not_found_in_trash’ => __( ‘No Showcases found in Trash.’, ‘showcase’ ),

‘featured_image’ => _x( ‘Showcase Cover Image’, ‘showcase’ ),

‘set_featured_image’ => _x( ‘Set cover image’, ‘showcase’ ),

‘remove_featured_image’ => _x( ‘Remove cover image’, ‘showcase’ ),

‘use_featured_image’ => _x( ‘Use as cover image’, ‘showcase’ ),

‘archives’ => _x( ‘Showcase archives’, ‘showcase’ ),

‘insert_into_item’ => _x( ‘Insert into Showcase’, ‘showcase’ ),

‘uploaded_to_this_item’ => _x( ‘Uploaded to this Showcase’, ‘showcase’ ),

‘filter_items_list’ => _x( ‘Filter Showcase list’, ‘showcase’ ),

‘items_list_navigation’ => _x( ‘Showcase list navigation’, ‘showcase’ ),

‘items_list’ => _x( ‘Showcase list’, ‘showcase’ ),

);



$args = array(

‘labels’ => $labels,

‘description’ => ‘Showcase custom post type.’,

‘public’ => true,

‘publicly_queryable’ => true,

‘show_ui' => true,

‘show_in_menu’ => true,

‘query_var’ => true,

‘rewrite’ => array( ‘slug’ => ‘showcase’ ),

‘capability_type’ => ‘post’,

‘has_archive’ => true,

‘hierarchical’ => false,

‘menu_icon’ => ‘dashicons-desktop’,

‘menu_position’ => 5,

‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’ ),

‘taxonomies’ => array( ‘category’, ‘post_tag’ ),

‘show_in_rest’ => true

);



register_post_type( ‘Showcase’, $args );

}

// Flushing Rewrite to get permalinks work

add_action( ‘after_switch_theme’, ‘devslife_rewrite_flush’ );

function devslife_rewrite_flush() {



devslife_showcase_post_type_init();

flush_rewrite_rules();



}

After you have saved the changes to the functions.php, a new menu item for your newly registered post type will appear in the admin menu.

New menu entry for your custom post type

Your new post type

Post type preview

You have just created the simplest version of a Custom Post Type. If you now create a new post of the new post type, you can use all Gutenberg blocks of your theme and, for example, enter a title and add headings, continuous text, images, videos, etc. Of course, you can now also publish your Custom Post Type.

Post Type Parameters that you can use for your post type

In our array of labels $labels we specify different key values for our post type. A full list of the label keys you can use for a post type can be found here: https://developer.wordpress.org/reference/functions/get_post_type_labels/

The second array consists of a series of arguments $args. A full list of arguments can be found here: https://developer.wordpress.org/reference/functions/register_post_type/

The basic structure for our new post type is in place. Now we can customize our post type with custom CSS, add custom fields and create special templates for our post type. In the 2nd part of this tutorial I will show you how.

Was this post useful to you? Would you like more tutorials on Devslife? I look forward to your suggestions and topic suggestions via email or comment.

Happy Coding : )