Steps to create a custom post type without the plugin.
Do you want to know how to make custom post types in WordPress quickly and easily? Custom post types turn a WordPress site into a robust Content Management System.
They essentially enable you to create different content types for your website in addition to posts and pages.
We’ll show you how to make custom post types in WordPress quickly and efficiently in this article.
In WordPress, what exactly is a Custom Post Type?
Content kinds such as posts and pages are examples of custom post types. The term “post” has persisted with WordPress as it grew from a simple blogging platform to a comprehensive CMS. A post type, on the other hand, can be any material.
These post kinds are included by default in WordPress:
- Post
- Page
- Attachment
- Revision
- Nav Menu
You have the freedom to name your own post kinds whatever you wish.
If you run a movie review website, for example, you’ll almost certainly want to establish a movie reviews post type. This post type can contain a variety of custom fields as well as its own category hierarchy.
Manually Creating a Custom Post Type
The problem with using a plugin is that when you deactivate it, your custom post kinds will vanish. Any data you have in those custom post types will remain intact, but they will be unregistered and no longer accessible from the admin area.
If you’re working on a client site and don’t want to install another plugin, you can create your own post type directly by including the necessary code in your theme’s functions.php file or in a site-specific plugin (See: Custom Post Types Debate functions.php or Plugin).
First, we’ll walk you through a fast and completely functional example so you can see how it works. Examine the following code:
// Our custom post type function
function create_posttype() {
register_post_type( ‘movies’,
// CPT Options
array(
‘labels’ => array(
‘name’ => __( ‘Movies’ ),
‘singular_name’ => __( ‘Movie’ )
),
‘public’ => true,
‘has_archive’ => true,
‘rewrite’ => array(‘slug’ => ‘movies’),
‘show_in_rest’ => true,
)
);
}
// Hooking up our function to theme setup
add_action( ‘init’, ‘create_posttype’ );
This code registers the post-type Services along with an array of parameters. Our custom post type choices are represented by these arguments.
The initial portion of this array is labeled, which is an array in itself. Other arguments in the second section include public visibility, which has an archive, slug, and show_in_rest, which enables block editor support.
Now let’s look at a more detailed piece of code that gives your custom post type extra choices.
/*
* Creating a function to create our Custom Post Type
*/
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
‘name’ => _x( ‘Services’, ‘Post Type General Name’, ‘twentytwenty’ ),
‘singular_name’ => _x( ‘Service’, ‘Post Type Singular Name’, ‘twentytwenty’ ),
‘menu_name’ => __( ‘Services’, ‘twentytwenty’ ),
‘parent_item_colon’ => __( ‘Parent Service’, ‘twentytwenty’ ),
‘all_items’ => __( ‘All Services’, ‘twentytwenty’ ),
‘view_item’ => __( ‘View Service’, ‘twentytwenty’ ),
‘add_new_item’ => __( ‘Add New Service’, ‘twentytwenty’ ),
‘add_new’ => __( ‘Add New’, ‘twentytwenty’ ),
‘edit_item’ => __( ‘Edit Service’, ‘twentytwenty’ ),
‘update_item’ => __( ‘Update Service’, ‘twentytwenty’ ),
‘search_items’ => __( ‘Search Service’, ‘twentytwenty’ ),
‘not_found’ => __( ‘Not Found’, ‘twentytwenty’ ),
‘not_found_in_trash’ => __( ‘Not found in Trash’, ‘twentytwenty’ ),
);
// Set other options for Custom Post Type
$args = array(
‘label’ => __( ‘Services’, ‘twentytwenty’ ),
‘description’ => __( ‘Service news and reviews’, ‘twentytwenty’ ),
‘labels’ => $labels,
// Features this CPT supports in Post Editor
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’, ‘comments’, ‘revisions’, ‘custom-fields’, ),
// You can associate this CPT with a taxonomy or custom taxonomy.
‘taxonomies’ => array( ‘genres’ ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
‘hierarchical’ => false,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘show_in_nav_menus’ => true,
‘show_in_admin_bar’ => true,
‘menu_position’ => 5,
‘can_export’ => true,
‘has_archive’ => true,
‘exclude_from_search’ => false,
‘publicly_queryable’ => true,
‘capability_type’ => ‘post’,
‘show_in_rest’ => true,
);
// Registering your Custom Post Type
register_post_type( ‘Services’, $args );
}
/* Hook into the ‘init’ action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( ‘init’, ‘custom_post_type’, 0 );
With this code, we’ve introduced a lot more choices to the custom post type, as you can see. Support for revisions, featured pictures, custom fields, and other features will be added.
This new post type is also linked to a custom taxonomy called genres.
Are you ready to publish your WordPress website today?
Let's Discuss Your Needs!