How to create custom post type manually in WordPress

Hello all,

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

Below are the default post type in WordPress

  • 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 stored in the same place “wp_posts” table. It’s differentiated by one column named “post_type”.

event there are many default post types we need some custom post type for our custom use. I am going to teach you how to do that.

In WordPress, there is 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 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 "
BOOK"
*/
 
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 );

5 Comments

  • Jeney
    May 30, 2020 - 13:29 pm

    Like!! Great article post.Really thank you! Really Cool.

  • erotik
    Dec 09, 2020 - 12:51 pm

    Thanks for sharing your thoughts. I truly appreciate your efforts and I will be waiting for your further post thank you once again. Elli Doy Elle

  • erotik
    Dec 09, 2020 - 16:17 pm

    There is apparently a lot to realize about this. I consider you made various good points in features also. Lian Arch Fairlie

  • porno
    Dec 09, 2020 - 19:30 pm

    Thanks-a-mundo for the article post. Much obliged. Clovis Gregory Skye

  • altyazili
    Jan 31, 2021 - 08:42 am

    It was truly informative. Your website is very useful. Crista Carmine Osyth Kitti Willy Emili

Leave A Reply