How to Register Custom Post Types & Taxonomies

One of the most powerful features of WordPress is its ability to go far beyond standard posts and pages. Whether you are building a real estate directory, a portfolio, an e-commerce catalog, or a client review system, you need to structure your data using Custom Post Types (CPTs) and Custom Taxonomies.

While many plugins handle this, registering them directly in your theme or custom plugin ensures cleaner code, better performance, and zero dependency bloat.

In this tutorial, we will walk through how to register a custom post type and a custom taxonomy the right way.

1. Registering a Custom Post Type (register_post_type)

To create a new post type, we hook into init and use register_post_type(). It is crucial to define proper labels, enable supports (like title, editor, thumbnail), and configure rewrite rules correctly.

Add the following code to your custom plugin or theme’s functions.php:

				
					if (!defined('ABSPATH')) {
    exit;
}

add_action('init', 'wg_register_custom_post_types');

function wg_register_custom_post_types() {
    $labels = [
        'name'               => __('Projects', 'webgarage'),
        'singular_name'      => __('Project', 'webgarage'),
        'add_new'            => __('Add New Project', 'webgarage'),
        'add_new_item'       => __('Add New Project Item', 'webgarage'),
        'edit_item'          => __('Edit Project', 'webgarage'),
        'new_item'           => __('New Project', 'webgarage'),
        'view_item'          => __('View Project', 'webgarage'),
        'search_items'       => __('Search Projects', 'webgarage'),
        'not_found'          => __('No projects found', 'webgarage'),
        'not_found_in_trash' => __('No projects found in trash', 'webgarage'),
        'menu_name'          => __('Projects', 'webgarage'),
    ];

    $args = [
        'labels'              => $labels,
        'public'              => true,
        'has_archive'         => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true, // Enables Gutenberg block editor support!
        'menu_icon'           => 'dashicons-portfolio',
        'supports'            => ['title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'],
        'rewrite'             => ['slug' => 'projects'],
        'capability_type'     => 'post',
    ];

    register_post_type('wg_project', $args);
}
				
			

2. Registering a Custom Taxonomy (register_taxonomy)

To categorize your new custom post type (similar to categories or tags), you use register_taxonomy(). You can make it hierarchical (like categories) or non-hierarchical (like tags).

Add this right below your post type registration:

				
					add_action('init', 'wg_register_custom_taxonomies');

function wg_register_custom_taxonomies() {
    $labels = [
        'name'              => __('Project Technologies', 'webgarage'),
        'singular_name'     => __('Technology', 'webgarage'),
        'search_items'      => __('Search Technologies', 'webgarage'),
        'all_items'         => __('All Technologies', 'webgarage'),
        'parent_item'       => __('Parent Technology', 'webgarage'),
        'parent_item_colon' => __('Parent Technology:', 'webgarage'),
        'edit_item'         => __('Edit Technology', 'webgarage'),
        'update_item'       => __('Update Technology', 'webgarage'),
        'add_new_item'      => __('Add New Technology', 'webgarage'),
        'new_item_name'     => __('New Technology Name', 'webgarage'),
        'menu_name'         => __('Technologies', 'webgarage'),
    ];

    $args = [
        'hierarchical'      => true, // Set to false for tag-like behavior
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'show_in_rest'      => true, // Enables Gutenberg support for terms
        'rewrite'           => ['slug' => 'project-technology'],
    ];

    // Bind taxonomy to our custom post type
    register_taxonomy('wg_tech', ['wg_project'], $args);
}
				
			

3. Flushing Rewrite Rules (Crucial Step)

Whenever you change post type slugs or add new registration code for the first time, WordPress rewrite rules need to be refreshed. Otherwise, you might encounter 404 Not Found errors when visiting your new post type archives or single pages.

You can temporarily trigger a flush by visiting Settings > Permalinks in your WordPress admin dashboard once after adding the code.

Registering custom post types and taxonomies manually gives you total control over your site’s data architecture, keeping your project lightweight and fully integrated with the WordPress REST API and Gutenberg editor.

Be sure to check out our previous tutorials on WordPress development:
Modifying WordPress Database Queries with posts_where
Creating Custom Cron Jobs in WordPress with WP-Cron
How to Use WordPress Transients API for Performance

Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.

Comments

No comments yet

Share Your Thoughts

Join the conversation!

Scroll to Top

Share Your Thoughts

Thank you for your comment!

Your email address will not be published. Required fields are marked *

Pro

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Standard

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Basic

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project