By default, WordPress comes with couple of post types
- Posts
- Revisions
- Pages
- Attachments
- Navigation Menus
But you can create your own custom post type to extend WordPress from simple blogging platform to powerful CMS(content management system).
No Code Solution
Coding Solution
To register a custom post type, we need to use the function register_post_type
provided by WordPress.
This function have 2-parameters, First is the post_type key which must be unique and be sure that not be conflict with other plugins and second/last parameter is array which contains labels and couple of configurations.
Let’s register a post-type for books collection
function my_custom_post_type() {
$labels = array(
'name' => _x('Books', 'post type general name'),
'singular_name' => _x('Book', 'post type singular name'),
'add_new' => _x('Add New', 'book'),
'add_new_item' => __('Add New Book'),
'edit_item' => __('Edit Book'),
'new_item' => __('New Book'),
'view_item' => __('View Book'),
'search_items' => __('Search Books'),
'not_found' => __('No books found'),
'not_found_in_trash' => __('No books found in Trash'),
'menu_name' => __('Books')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array('slug' => 'book'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-book',
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments')
);
register_post_type('book', $args);
}
add_action('init', 'my_custom_post_type');
PHPUnderstand the Parameters
- labels: An array defining various text labels for the post type.
- public: Whether the post type is intended to be public or not.
- publicly_queryable: Whether queries can be made for this post type.
- show_ui: Whether to generate a default UI for managing this post type.
- show_in_menu: Whether to show this post type in the admin menu.
- show_in_rest: Whether posts can be available on rest API.
- query_var: Whether to enable the query variable.
- rewrite: Array for custom URL slug.
- capability_type: Defines what capabilities are needed to manage this post type.
- has_archive: Whether to generate an archive page for this post type.
- hierarchical: Whether the post type is hierarchical (like pages) or not.
- menu_position: The position in the admin menu.
- menu_icon: A icon which display for post type on admin menu
- supports: Features supported by the post type (e.g., title, editor, thumbnail).
Custom taxonomies
Let’s create custom a taxonomy called “genre” for Books post type.
function my_custom_taxonomy() {
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genres' ),
'not_found' => __( 'No genres found' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'book' ), $args );
}
add_action( 'init', 'my_custom_taxonomy' );
PHPregister_taxonomy function: function, the first parameter is the taxonomy name genre, the second is an array of post types it will be associated with book
, and the third is the arguments array.
Understand the arguments parameter
- labels: An array defining various text labels for the taxonomy.
- hierarchical: Whether the taxonomy behaves like categories (true) or tags (false).
- show_ui: Whether to generate a default UI for the taxonomy in the admin panel.
- show_in_rest: Whether taxonomy can be available on rest API.
- show_admin_column: Whether to display the taxonomy column in the admin post list.
- query_var: Whether to enable the query variable.
- rewrite: Array for custom URL slug.
Let’s create a book with genre
If you’re using Gutenberg based block theme, you can set the “Genre” term from Categories terms block. If you didn’t found the option, make sure you’ve set true show_in_rest
on post type and taxonomy both.
Preview
Thanks.
Leave a Comment