How to Create Custom Post Type Manually in WordPress

How to Create Custom Post Type Manually in WordPress

Imagine WordPress as a big toolbox for making websites. Inside this toolbox, there’s a special tool called “custom post types.” These are like magic spells that help you create different kinds of content, not just the usual articles and pages. Whether you want to show off your artwork, make a calendar for events, list products to sell, or do anything else you dream up, custom post types are here to make it real. So, let’s explore how you can learn the magical way of crafting your very own custom post type!

Many people are finding a solution to how to create custom post types manually or programmatically in WordPress. First, you have to know what is the post in WordPress and how many seats are available in WordPress. In WordPress content is available in many different types. This type of content we can describe as Post Types.

What are Custom Post Types?

Think of post types as containers for specific kinds of content on your website. By default, WordPress offers two main types: “posts” for your blog articles and “pages” for static content like About and Contact pages. But sometimes, these just aren’t enough to showcase your unique content.

Custom post types allow you to define and manage various content structures. Imagine you have a travel blog and want to highlight your favorite destinations separately from your regular blog posts.

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

There are many different post types but all are stored in the same “wp_posts” table. It’s differentiated by one column named “post_type” event there are many default post types we need some custom post types for our custom use. I am going to teach you how to do that.

WordPress has a default function to create the custom post type.

register_post_type( string $post_type, array|string $args = array() )

Now, We are going to create a custom post type named “Book” using this function

* Creating a function to create custom post type. 
Here we are going to create post type named "
function register_book() {
// Set UI labels for Custom Post Type, This labels will be visible to add,
 view or edit post
    $labels = array(
        'name'                => _x( 'Books', 'Post Type General Name', 'my_slug' ),
        'singular_name'       => _x( 'Book', 'Post Type Singular Name', 'my_slug' ),
        'menu_name'           => __( 'Books', 'my_slug' ),
        'parent_item_colon'   => __( 'Parent Book', 'my_slug' ),
        'all_items'           => __( 'All Books', 'my_slug' ),
        'view_item'           => __( 'View Book', 'my_slug' ),
        'add_new_item'        => __( 'Add New Book', 'my_slug' ),
        'add_new'             => __( 'Add New', 'my_slug' ),
        'edit_item'           => __( 'Edit Book', 'my_slug' ),
        'update_item'         => __( 'Update Book', 'my_slug' ),
        'search_items'        => __( 'Search Book', 'my_slug' ),
        'not_found'           => __( 'Not Found', 'my_slug' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'my_slug' ),
// Set other options for Custom Post Type
    $args = array(
        'label'               => __( 'books', 'my_slug' ),
        'description'         => __( 'Book', 'my_slug' ),
        '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( 'books', $args );
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
add_action( 'init', 'register_book', 0 );

Creating custom post types in WordPress isn’t just about adding new content; it’s about tailoring your website to your vision. By following this guide, you’ve embarked on a journey to make your WordPress site even more powerful and customized. Now you can organize and present your content like never before, giving your visitors a richer and more engaging experience. Happy spellcasting!

Leave a Reply

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