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:

 

Related Blogs

Convert PHP Array into JavaScript Array.

To convert both a normal PHP array, a multidimensional PHP array, and a nested PHP array into JavaScript arrays, you can use JSON encoding and decoding as previously explained. JSON is well-suited for handling various data structures, including both simple arrays and complex nested arrays.

Add Custom Taxonomy Filter on Products Dashboard.

How To Filter Products by Taxonomies in the Dashboard?

WooCommerce provide Many Product filters on admin screen , such as “Select a category”, “Filter by product type”, “Filter by stock status”.

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

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.