Paramclasses Academy - Blog

Tutorials Details

←
Post 35 of 40
→

How to Create a Custom Post Type and Custom Taxonomy in WordPress ?

How to Create a Custom Post Type and Custom Taxonomy in WordPress ?

November 17, 2026

1. Introduction

WordPress provides built-in content types such as Posts and Pages. However, when developing professional websites, we often need to manage different types of content separately.

For example:

  • A portfolio website needs Projects.

  • An online bookstore needs Books.

  • An educational website needs Courses.

  • A movie review website needs Movies.

  • A business website may need Testimonials.

Creating all this content using ordinary Posts or Pages can make website management difficult.

WordPress solves this problem with two powerful features:

  1. Custom Post Type (CPT): Creates a new content type in WordPress.

  2. Custom Taxonomy: Organizes and categorizes that content.

In this tutorial, we will build a practical example of a Book Management section with:

  • A custom post type named Books.

  • A custom taxonomy named Book Genres.

  • Different genre terms such as Programming, Web Development and Database.

  • Custom frontend templates to display books.

By the end, you will understand how developers create structured content systems in WordPress.

2. What Is a Post Type in WordPress?

A Post Type defines the type of content stored and managed by WordPress.

WordPress includes several built-in post types:

Post Type Purpose
post Blog articles
page Static website pages
attachment Uploaded media
revision Previous content versions
nav_menu_item Navigation menu entries

 

For example:

  • About Us is generally a Page.

  • A blog article is generally a Post.

  • A book listing can be a Custom Post Type.

3. What Is a Custom Post Type?

A Custom Post Type is a content type that developers create according to a website's requirements.

Suppose you are developing an online bookstore.

Instead of adding every book as a normal blog post, you can create a separate Books section in the WordPress dashboard.

This section can manage:

  • Book titles

  • Book descriptions

  • Featured images

  • Authors

  • Publication information

  • Book categories or genres

The WordPress dashboard will display a new Books menu where administrators can add, edit and delete book entries.

4. What Is a Custom Taxonomy?

A Taxonomy is a system for organizing and grouping content.

WordPress already provides two built-in taxonomies:

  • Categories

  • Tags

A Custom Taxonomy allows developers to create their own classification system.

For example, our Books section can have a custom taxonomy called Book Genres.

Its terms could include:

  • Programming

  • Web Development

  • Database

  • Graphic Design

  • Networking

Difference Between Custom Post Type and Custom Taxonomy

Custom Post Type Custom Taxonomy
Creates a content type Organizes content
Example: Books Example: Book Genres
Stores individual entries Stores classification terms
Created using register_post_type() Created using register_taxonomy()

Remember:

Custom Post Type creates the content section, while Custom Taxonomy organizes that content.

5. Prepare Your WordPress Development Environment

Before starting, make sure you have:

  1. A working WordPress installation.

  2. Administrator access to the dashboard.

  3. Access to your WordPress files.

  4. A code editor such as VS Code.

For this practical example, we will create a small custom plugin.

Why should we use a plugin instead of a theme?

WordPress recommends registering custom post types in a plugin.

If you register your Books post type inside a theme and later change the theme, the Books menu may disappear.

When you register it through a plugin, your content structure remains available even after changing the theme.

Official documentation: Registering Custom Post Types.

6. Create a Custom Plugin

Open your WordPress project folder.

Navigate to:

wp-content/plugins/

Create a new folder:

param-book-manager

Inside this folder, create a PHP file:

param-book-manager.php

Your folder structure should look like this:

wordpress/
│
└── wp-content/
    │
    └── plugins/
        │
        └── param-book-manager/
            │
            └── param-book-manager.php

Open param-book-manager.php and add the following plugin header:

<?php

/**
 * Plugin Name: Param Book Manager
 * Description: Creates Books custom post type and Book Genres taxonomy.
 * Version: 1.0.0
 * Author: Param Classes
 */

Save the file.

Now open your WordPress dashboard.

Navigate to:

Dashboard → Plugins → Installed Plugins

Find Param Book Manager and click Activate.

Your custom plugin is now activated.

7. Create a Custom Post Type Using register_post_type()

WordPress provides the register_post_type() function to create custom post types.

Its basic structure is:

register_post_type(
    'post_type_name',
    array(
        'label' => 'Post Type Name',
        'public' => true
    )
);

Important arguments include:

  • labels: Defines dashboard labels.

  • public: Controls public visibility and administrative availability.

  • has_archive: Enables an archive page.

  • supports: Defines available editor features.

  • rewrite: Controls URL structure.

  • show_in_rest: Enables REST API integration, including block editor support.

Official documentation: register_post_type().

Step 1: Register the Books Post Type

Open param-book-manager.php.

Add this code below the plugin header:

function param_register_books_post_type() {

    $labels = array(

        'name'          => 'Books',
        'singular_name' => 'Book',

        'add_new'       => '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',

        'all_items'     => 'All Books',

        'menu_name'     => 'Books'

    );

    $args = array(

        'labels' => $labels,

        'public' => true,

        'has_archive' => true,

        'rewrite' => array(
            'slug' => 'books'
        ),

        'supports' => array(
            'title',
            'editor',
            'thumbnail',
            'excerpt',
            'author',
            'revisions'
        ),

        'show_in_rest' => true,

        'menu_icon' => 'dashicons-book'

    );

    register_post_type('param_book', $args);

}

add_action('init', 'param_register_books_post_type');

Step 2: Understand the Code

1. $labels

Defines the text displayed in the WordPress dashboard.

For example:

  • Books

  • Add New Book

  • Edit Book

  • All Books

2. public => true

Makes the post type publicly accessible and available in the administrative interface.

3. has_archive => true

Enables the main archive page for Books.

Example:

https://example.com/books/

4. rewrite

Defines the URL slug for this post type.

5. supports

Enables features such as:

  • Title

  • Content editor

  • Featured image

  • Excerpt

  • Author

  • Revisions

6. show_in_rest => true

Makes the post type available through the WordPress REST API and enables compatibility with the Gutenberg block editor.

7. menu_icon

Sets the icon displayed next to Books in the dashboard sidebar.

8. add_action('init', ...)

Runs the registration function during WordPress initialization.

The init hook is the recommended place to register post types.

8. Create a Custom Taxonomy Using register_taxonomy()

Now we will create a custom taxonomy called Book Genres.

WordPress provides the register_taxonomy() function for this purpose.

Basic syntax:

register_taxonomy(
    'taxonomy_name',
    array('post_type_name'),
    array(
        'label' => 'Taxonomy Name',
        'public' => true
    )
);

Official documentation: register_taxonomy().

Step 1: Register the Book Genres Taxonomy

Add this function to the same plugin file:

function param_register_book_genres() {

    $labels = array(

        'name'              => 'Book Genres',
        'singular_name'     => 'Book Genre',

        'search_items'      => 'Search Book Genres',

        'all_items'         => 'All Book Genres',

        'parent_item'       => 'Parent Book Genre',

        'parent_item_colon' => 'Parent Book Genre:',

        'edit_item'         => 'Edit Book Genre',

        'update_item'       => 'Update Book Genre',

        'add_new_item'      => 'Add New Book Genre',

        'new_item_name'     => 'New Book Genre Name',

        'menu_name'         => 'Book Genres'

    );

    $args = array(

        'labels' => $labels,

        'public' => true,

        'hierarchical' => true,

        'show_ui' => true,

        'show_admin_column' => true,

        'show_in_rest' => true,

        'rewrite' => array(
            'slug' => 'book-genre'
        )

    );

    register_taxonomy(
        'param_book_genre',
        array('param_book'),
        $args
    );

}

add_action('init', 'param_register_book_genres');

Save your plugin file.

Step 2: Understand the Taxonomy Code

1. Taxonomy Name

param_book_genre is the internal taxonomy identifier.

2. Object Type

array('param_book') connects this taxonomy with our Books custom post type.

3. Hierarchical

true makes the taxonomy behave like Categories.

It supports parent and child terms.

4. show_admin_column

Displays the assigned Book Genres in the Books listing table.

5. show_in_rest

Enables REST API and block editor integration.

6. rewrite

Defines the URL slug for taxonomy archive pages.

9. Understand Hierarchical and Non-Hierarchical Taxonomies

WordPress supports two common taxonomy structures.

A. Hierarchical Taxonomy

A hierarchical taxonomy supports parent and child terms.

Example:

Book Genres
│
├── Programming
│   ├── PHP
│   ├── Java
│   └── Python
│
├── Web Development
│   ├── HTML
│   ├── CSS
│   └── JavaScript
│
└── Database
    ├── MySQL
    └── MongoDB

Use:

'hierarchical' => true

This is similar to WordPress Categories.

B. Non-Hierarchical Taxonomy

A non-hierarchical taxonomy uses independent terms rather than parent-child relationships.

Example:

Book Tags

PHP
Beginner
Advanced
Coding
Web Design

Use:

'hierarchical' => false

This is similar to WordPress Tags.

10. Understand the Complete Plugin Structure

Your plugin now contains two registration functions.

param-book-manager/
│
└── param-book-manager.php
    │
    ├── Plugin Header
    │
    ├── param_register_books_post_type()
    │
    └── param_register_book_genres()

Both functions run on the init hook.

For reliable registration, register the post type before registering its taxonomy, as shown in the code above.

An additional option is to include the taxonomy identifier in the post type's taxonomies argument when registering the post type. This helps WordPress recognize the relationship in query-related operations. The taxonomy itself must still be registered separately.

11. Add Books from the WordPress Dashboard

Now open:

Dashboard → Books → Add New Book

You should see the following fields:

  • Book Title

  • Content Editor

  • Featured Image

  • Excerpt

  • Book Genres

Enter a sample book:

Title: PHP for Beginners

Description: Learn PHP programming fundamentals with practical examples.

Genre: Programming

Add more sample books:

Book Title Book Genre
PHP for Beginners Programming
HTML and CSS Guide Web Development
MySQL Complete Guide Database
JavaScript Fundamentals Web Development

Click Publish to save each book.

12. Create Custom Post Type Frontend Templates

Registering a Custom Post Type creates the content section, but you may also want to design how its content appears on your website.

WordPress uses Template Hierarchy to select the appropriate template.

For a classic PHP theme, create these files:

your-theme/
│
├── functions.php
│
├── single-param_book.php
│
├── archive-param_book.php
│
└── taxonomy-param_book_genre.php

These files have different purposes:

Template File Purpose
single-param_book.php Displays one book
archive-param_book.php Displays all books
taxonomy-param_book_genre.php Displays books belonging to a genre

 

 
   

Official documentation: Custom Post Type Template Files and Template Hierarchy.

Note: These PHP templates apply to classic themes. Block themes use the corresponding .html template hierarchy.

Step 1: Create the Single Book Template

Create:

single-param_book.php

Add:

<?php get_header(); ?>

<main class="book-single">

    <?php
    if (have_posts()) :

        while (have_posts()) :

            the_post();
    ?>

        <article>

            <h1>
                <?php the_title(); ?>
            </h1>

            <?php
            if (has_post_thumbnail()) {
                the_post_thumbnail('large');
            }
            ?>

            <div class="book-content">

                <?php the_content(); ?>

            </div>

        </article>

    <?php
        endwhile;

    endif;
    ?>

</main>

<?php get_footer(); ?>

This template displays:

  • Book title

  • Featured image

  • Book description

Step 2: Create the Books Archive Template

Create:

archive-param_book.php

Add:

<?php get_header(); ?>

<main class="books-archive">

    <h1>Our Books</h1>

    <?php if (have_posts()) : ?>

        <div class="books-list">

            <?php while (have_posts()) : the_post(); ?>

                <article>

                    <h2>

                        <a href="<?php echo esc_url(get_permalink()); ?>">

                            <?php the_title(); ?>

                        </a>

                    </h2>

                    <?php
                    if (has_post_thumbnail()) {
                        the_post_thumbnail('medium');
                    }
                    ?>

                    <div>

                        <?php the_excerpt(); ?>

                    </div>

                </article>

            <?php endwhile; ?>

        </div>

        <?php the_posts_pagination(); ?>

    <?php else : ?>

        <p>No books found.</p>

    <?php endif; ?>

</main>

<?php get_footer(); ?>

This template displays all published books in the archive.

Step 3: Create the Book Genre Template

Create:

taxonomy-param_book_genre.php

Add:

<?php get_header(); ?>

<main class="book-genre-archive">

    <h1>
        <?php single_term_title(); ?>
    </h1>

    <?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

            <article>

                <h2>

                    <a href="<?php echo esc_url(get_permalink()); ?>">

                        <?php the_title(); ?>

                    </a>

                </h2>

                <div>

                    <?php the_excerpt(); ?>

                </div>

            </article>

        <?php endwhile; ?>

        <?php the_posts_pagination(); ?>

    <?php else : ?>

        <p>No books found in this genre.</p>

    <?php endif; ?>

</main>

<?php get_footer(); ?>

This template displays books associated with the selected genre.

13. Display Custom Post Type Data Using WP_Query

Sometimes you want to display Books on a custom homepage or another page.

For this purpose, WordPress provides WP_Query.

Example:

<?php

$args = array(

    'post_type'      => 'param_book',

    'posts_per_page' => 6,

    'post_status'   => 'publish'

);

$books = new WP_Query($args);

?>

<?php if ($books->have_posts()) : ?>

    <?php while ($books->have_posts()) : $books->the_post(); ?>

        <h2>

            <a href="<?php echo esc_url(get_permalink()); ?>">

                <?php the_title(); ?>

            </a>

        </h2>

        <?php the_excerpt(); ?>

    <?php endwhile; ?>

<?php else : ?>

    <p>No books available.</p>

<?php endif; ?>

<?php wp_reset_postdata(); ?>

Understand WP_Query

  • post_type: Selects the content type.

  • posts_per_page: Controls the number of results.

  • post_status: Selects published entries.

  • have_posts(): Checks whether results exist.

  • the_post(): Sets up the current post for template functions.

  • wp_reset_postdata(): Restores the original global post data after a secondary query.

Official documentation: WP_Query.

14. Filter Books Using a Custom Taxonomy

You can also retrieve books belonging to a specific genre using tax_query.

Example:

<?php

$args = array(

    'post_type' => 'param_book',

    'posts_per_page' => 10,

    'tax_query' => array(

        array(

            'taxonomy' => 'param_book_genre',

            'field' => 'slug',

            'terms' => 'programming'

        )

    )

);

$books = new WP_Query($args);

?>

<?php if ($books->have_posts()) : ?>

    <?php while ($books->have_posts()) : $books->the_post(); ?>

        <h2><?php the_title(); ?></h2>

    <?php endwhile; ?>

<?php else : ?>

    <p>No programming books found.</p>

<?php endif; ?>

<?php wp_reset_postdata(); ?>

This query retrieves Books assigned to the programming genre term.

Important: Create the Programming term in the dashboard and make sure its slug is programming before testing this example.

15. Flush WordPress Permalinks

After registering a Custom Post Type, sometimes its archive or single URLs return a 404 error.

For example:

https://example.com/books/

To refresh the rewrite rules:

  1. Open the WordPress dashboard.

  2. Navigate to Settings.

  3. Click Permalinks.

  4. Do not change anything.

  5. Click Save Changes.

WordPress will refresh its permalink rules.

Do not call flush_rewrite_rules() on every page load. It is an expensive operation. In a production plugin, rewrite rules should be flushed during plugin activation and deactivation when appropriate.

16. Common Errors and Their Solutions

Error 1: Books Menu Is Not Showing

Possible reasons:

  • Plugin is not activated.

  • PHP syntax error.

  • Incorrect registration function.

  • init hook is missing.

Solution:

Check the plugin file, activate the plugin and verify the function name.

Error 2: Books Page Shows 404 Error

Reason: WordPress rewrite rules may not be refreshed.

Solution: Open Settings → Permalinks and click Save Changes.

Error 3: Gutenberg Editor Is Not Showing

Reason: The Custom Post Type may not have REST API support enabled.

Solution:

'show_in_rest' => true

Also ensure that the editor is included in the supports array.

Error 4: Book Genres Are Not Showing

Possible reasons:

  • Taxonomy registration function is missing.

  • Incorrect taxonomy name.

  • Incorrect post type association.

  • Taxonomy is not registered properly.

Solution:

Verify that the taxonomy is associated with param_book and that both registration functions run on init.

Error 5: Custom Template Is Not Loading

Reason: The template filename may be incorrect.

Solution:

Check the exact filenames:

single-param_book.php
archive-param_book.php
taxonomy-param_book_genre.php

Also make sure these files are in the active classic theme's root directory.

Error 6: Custom Post Type Disappears After Changing Themes

Reason: The CPT was registered inside the previous theme.

Solution: Register it in a custom plugin so that the content structure is independent of the active theme.

17. Best Practices for WordPress Developers

  1. Register Custom Post Types in plugins rather than themes.

  2. Use unique prefixes for function names and identifiers.

  3. Keep post type keys within the 20-character limit.

  4. Use lowercase identifiers and avoid reserved WordPress post type names.

  5. Enable show_in_rest when block editor and REST API support are required.

  6. Use meaningful taxonomy names.

  7. Avoid creating unnecessary taxonomies.

  8. Use the correct template hierarchy.

  9. Escape output when displaying dynamic data.

  10. Avoid flushing rewrite rules on every page request.

  11. Test archive, single and taxonomy pages.

  12. Use appropriate capabilities and permissions for websites with multiple user roles.

18. Practical Assignment

Now create your own Custom Post Type project.

Project Name: Param Classes Course Management System

Your task is to create a Courses section in WordPress.

Requirements:

  1. Create a custom post type named param_course.

  2. Set its dashboard label to Courses.

  3. Enable title, editor, featured image and excerpt.

  4. Enable archive support.

  5. Enable REST API support.

  6. Create a custom taxonomy named param_course_category.

  7. Add these taxonomy terms:

    • WordPress Development

    • PHP Development

    • Android Development

    • Web Design

  8. Add at least five sample courses.

  9. Create a single course template.

  10. Create a course archive template.

  11. Display courses using WP_Query.

  12. Test the taxonomy archive pages.

Expected Result: Your WordPress dashboard should have a separate Courses section, and visitors should be able to browse courses according to their categories.

19. Frequently Asked Questions

Q1. What is a Custom Post Type in WordPress?

A Custom Post Type is a custom content type used to manage content separately from standard Posts and Pages.

Q2. Which function creates a Custom Post Type?

WordPress provides the register_post_type() function.

Q3. Which function creates a Custom Taxonomy?

The register_taxonomy() function creates custom taxonomies.

Q4. What is the difference between a Post Type and a Taxonomy?

A Post Type defines the content structure, whereas a Taxonomy organizes that content into groups.

Q5. Can we create multiple Custom Post Types?

Yes. You can register multiple custom post types for different content requirements.

Q6. Can a Custom Taxonomy be connected to multiple Post Types?

Yes. You can associate one taxonomy with multiple post types by passing multiple post type identifiers to register_taxonomy().

Q7. Why should we use show_in_rest?

It enables REST API access and supports the Gutenberg block editor for the registered content type or taxonomy.

Q8. Where should we register Custom Post Types?

A custom plugin or must-use plugin is generally recommended so that the content structure remains available when changing themes.

Q9. What is WP_Query?

WP_Query is a WordPress class used to retrieve posts according to specified query parameters.

Q10. What is the purpose of tax_query?

It filters query results according to taxonomy terms.

20. Conclusion

In this tutorial, we learned how to create Custom Post Types and Custom Taxonomies in WordPress using PHP.

We covered:

  • WordPress built-in post types.

  • Custom Post Types.

  • Custom Taxonomies.

  • register_post_type().

  • register_taxonomy().

  • Hierarchical and non-hierarchical taxonomies.

  • Custom plugin development.

  • Single and archive templates.

  • WP_Query and tax_query.

  • Permalink configuration.

  • Common errors and best practices.

Custom Post Types and Taxonomies are important WordPress development concepts. They allow developers to build structured, scalable and professional websites instead of relying only on standard Posts and Pages.

Final Learning Outcome: You can now create your own content management structure in WordPress using PHP and display that content through custom templates.

←
Post 35 of 40
→