How To Add New Post Type and Custom Taxonomy in WordPress

How To Add New Post Type and Custom Taxonomy in WordPress

WordPress allows you to create custom post types and taxonomies, offering greater flexibility for organizing and displaying content. The register_post_type and register_taxonomy functions are essential for this purpose. In this guide, we will walk you through the steps to add a new post type and a custom taxonomy.

 

Step 1: Register a Custom Post Type

In this example, let’s create a custom post type called “Books”.

function custom_book_post_type() {
    $labels = array(
        'name'               => 'Books',
        'singular_name'      => 'Book',
        'menu_name'          => 'Books',
        'name_admin_bar'     => 'Book',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Book',
        'edit_item'          => 'Edit Book',
        'new_item'           => 'New Book',
        'view_item'          => 'View Book',
        'all_items'          => 'All Books',
        'search_items'       => 'Search Books',
        'not_found'          => 'No books found',
        'not_found_in_trash' => 'No books found in trash',
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'books'),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
    );

    register_post_type('book', $args);
}
add_action('init', 'custom_book_post_type');

Step 2: Register a Custom Taxonomy

Next, let’s create a custom taxonomy called “Genres” and associate it with the “Books” custom post type.

function custom_book_genres_taxonomy() {
    $labels = array(
        'name'                       => 'Genres',
        'singular_name'              => 'Genre',
        'search_items'               => 'Search Genres',
        'popular_items'              => 'Popular Genres',
        'all_items'                  => 'All Genres',
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => 'Edit Genre',
        'update_item'                => 'Update Genre',
        'add_new_item'               => 'Add New Genre',
        'new_item_name'              => 'New Genre Name',
        'separate_items_with_commas' => 'Separate genres with commas',
        'add_or_remove_items'        => 'Add or remove genres',
        'choose_from_most_used'      => 'Choose from the most used genres',
        'not_found'                  => 'No genres found.',
        'menu_name'                  => 'Genres',
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'genre'),
    );

    register_taxonomy('genre', 'book', $args);
}
add_action('init', 'custom_book_genres_taxonomy');

 

With these code snippets, you’ve successfully created a new custom post type called “Books” and a custom taxonomy called “Genres”, associated with the “Books” post type. You can now manage your “Books” content in the WordPress dashboard under the new “Books” menu item. Additionally, you can assign genres to each book using the “Genres” taxonomy. Feel free to customize the labels, slugs, and parameters to meet your specific needs.

Final Thoughts

Custom post types and taxonomies provide immense flexibility when managing content in WordPress. By using the register_post_type and register_taxonomy functions, you can organize and display content in a way that best fits your website’s structure and user needs.

 

External Links & Further Reading

If you’d like to learn more about custom post types, taxonomies, and their advanced features, here are some valuable resources:

 

Categories

Related Blogs

How To Add Sub Menu In WordPress Admin Panel

How to add sub-menu in WordPress admin panel – In this post we see how we can add custom sub-menu to admin sidebar. Sometimes when we on WordPress and we need to show some features or any information in admin page then we can use this code snippet to create the same.

WooCommerce: Display Regular & Sale Price @ Cart Table

Learn how to enhance your WooCommerce cart table by displaying both the regular and sale prices. Implement this functionality using WordPress hooks and filters to provide clear and appealing pricing information to your customers, encouraging sales and improving the shopping experience.

Request A Quote