Extending WordPress: Custom Post Types and Taxonomies

To transform WordPress from a simple blog into a powerful CMS, you need to define your own content structures. If you are building a library site, "Posts" isn't enoughβ€”you need "Books." If you are building a portfolio, you need "Projects." By manually registering Custom Post Types (CPT) and Taxonomies, you gain full control over the admin interface, URL structures (slugs), and how content is categorized. This guide provides the boilerplate code needed to register a "Books" post type and a "Genres" taxonomy, providing a foundation for any complex WordPress project.

Add Custom Post Types and Taxonomies in WordPress

Content Management Theme Development Wordpress WordPress Development WordPress Functions WordPress Tips

Add Custom Post Types and Taxonomies in WordPress Tutorial/Guide

Enhancing WordPress functionality often involves creating custom content types. By using register_post_type and register_taxonomy, you can build tailored solutions for your site. Let’s go through the process together.  

Step 1: Define the Post Type "Books"

We'll register a new post type named Books using the function below:

function create_books_post_type() {
    $labels = array(
        'name'               => 'Books',
        'singular_name'      => 'Book',
        'menu_name'          => 'Books',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Book',
        'edit_item'          => 'Edit Book',
        'view_item'          => 'View Book',
        'all_items'          => 'All Books',
        'search_items'       => 'Search Books',
        'not_found'          => 'No Books Found',
        'not_found_in_trash' => 'No Books in Trash',
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'has_archive'        => true,
        'rewrite'            => array('slug' => 'books'),
        'supports'           => array('title', 'editor', 'thumbnail'),
    );

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

Step 2: Create the "Genres" Taxonomy

Now, let’s define a custom taxonomy called Genres for classifying books.

function register_genres_taxonomy() {
    $labels = array(
        'name'               => 'Genres',
        'singular_name'      => 'Genre',
        'search_items'       => 'Search Genres',
        'all_items'          => 'All Genres',
        'edit_item'          => 'Edit Genre',
        'add_new_item'       => 'Add New Genre',
        'new_item_name'      => 'New Genre Name',
        'menu_name'          => 'Genres',
    );

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

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

  Your WordPress site is now ready to handle "Books" as a content type, and "Genres" as their categories. This adds a whole new level of content organization.

Final Thoughts

WordPress customization using post types and taxonomies is a powerful way to expand your site. These functions open doors to fully structured and manageable content types.

Further Resources

πŸ’‘ Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.