Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 10 of the WordPress Plugin Development: Foundations (PHP & MVC) course
WordPressJune 25, 20263 min read

Registering Custom Post Types: A WordPress Developer’s Guide

Master the art of registering a custom post type (CPT) in WordPress. Learn to use register_post_type to structure your plugin data effectively today.

WordPressPHPPlugin DevelopmentCPTCustom Post TypeRegistrationplugin-development

Previously in this course, we explored The Controller Layer for Admin Pages, where we learned how to route menu requests and keep our admin logic organized. In this lesson, we shift our focus from UI management to data structure by defining a Custom Post Type (CPT).

A CPT allows you to store specific types of content—like our "Knowledge Articles"—separately from regular blog posts, giving you full control over how that data is managed, queried, and displayed within WordPress.

Understanding Custom Post Type Registration

In WordPress, everything is a post. Whether it's a page, an attachment, or a blog post, they all reside in the wp_posts table. A CPT is simply a way to flag a row in that table with a unique identifier (the post_type).

When you register a CPT, you aren't creating a new database table; you are telling WordPress how to treat a specific category of posts. This unlocks the full power of the WordPress ecosystem, including the editor, taxonomies, and the native query engine.

The register_post_type Function

The primary tool for this is register_post_type(). It accepts two arguments: the post type name (a string, max 20 characters) and an array of arguments that define its behavior.

To keep our plugin architecture clean, we will implement this within a dedicated method in our model layer or a specific registration class, ensuring we hook into the init action.

Worked Example: Registering the Knowledge Article CPT

Let’s define our "Knowledge Article" CPT. We want this to be public, searchable, and manageable in the admin dashboard.

PHP
#6A9955">/**
 * Register the Knowledge Article CPT.
 */
public function register_knowledge_articles() {
    $labels = [
        'name'          => 'Knowledge Articles',
        'singular_name' => 'Knowledge Article',
        'add_new'       => 'Add New Article',
        'edit_item'     => 'Edit Article',
    ];

    $args = [
        'label'             => 'Knowledge Articles',
        'labels'            => $labels,
        'public'            => true,
        'has_archive'       => true,
        'rewrite'           => ['slug' => 'knowledge-base'],
        'supports'          => ['title', 'editor', 'thumbnail'],
        'capabilities'      => ['edit_posts' => 'edit_posts'],
        'show_in_rest'      => true, #6A9955">// Required for Gutenberg support
    ];

    register_post_type('kb_article', $args);
}

#6A9955">// Hook into the 'init' action
add_action('init', [$this, 'register_knowledge_articles']);

Key Components of the Registration Array

  • Labels: These define the strings users see in the dashboard. Providing these improves the user experience significantly.
  • Public: Setting this to true makes the post type available in the admin and potentially the front end.
  • Show in REST: Always set this to true if you want your CPT to work with the block editor (Gutenberg).
  • Capabilities: By default, WordPress maps CPT capabilities to post capabilities. You can create custom ones, but for most plugins, sticking to the standard edit_posts mapping is sufficient.

Hands-on Exercise

  1. Open your plugin's primary model class (or create a PostTypeManager class).
  2. Add the register_knowledge_articles method as shown above.
  3. Ensure the init action is triggered correctly within your plugin's lifecycle.
  4. Activate your plugin, then navigate to your WordPress dashboard. You should see "Knowledge Articles" in the sidebar.
  5. Crucial Step: After registering a CPT, visit Settings > Permalinks and click "Save Changes." This flushes the rewrite rules, ensuring your new CPT URLs work immediately.

Common Pitfalls

  • Forgetting to Flush Permalinks: If you get a 404 error when visiting a new CPT URL, it’s almost always because the rewrite rules haven't been flushed.
  • Reserved Names: Avoid using post type names that WordPress reserves, such as post, page, attachment, revision, or nav_menu_item.
  • Missing 'show_in_rest': If you don't set show_in_rest to true, your CPT will fall back to the classic editor instead of the modern block editor.
  • Namespace Collisions: Always prefix your CPT name (e.g., kb_article instead of just article) to prevent conflicts with other plugins or themes.

Recap

We have successfully extended the WordPress data structure by registering a custom post type. By using register_post_type, we’ve enabled a dedicated space for our Knowledge Base content, configured its admin labels, and ensured compatibility with the block editor. This provides a robust foundation for the CRUD operations we will implement later in the course.

Up next: Configuring CPT Arguments, where we will dive deeper into fine-tuning the behavior of our Knowledge Article CPT, including custom rewrite slugs and hierarchical support.

Previous lessonThe Controller Layer for Admin PagesNext lesson Configuring CPT Arguments
Back to Blog

Similar Posts

WordPressWordPressJune 25, 20263 min read

REST API Integration: Exposing Data for External Consumption

Learn to extend the WordPress REST API by registering custom endpoints. We'll show you how to securely serve your Knowledge Base data as structured JSON.

Read more
WordPressWordPressJune 25, 2026

Part of the course

WordPress Plugin Development: Foundations (PHP & MVC)

beginner · Lesson 10 of 47

  1. 1

    Plugin Anatomy and File Structure

    3 min
  2. 2

    The Plugin Lifecycle Hooks

    4 min
  3. 3

    Designing for MVC in WordPress

    3 min
3 min read

Capability Checks: Securing WordPress Plugins with Authorization

Master WordPress security by implementing capability checks. Learn to use current_user_can to restrict admin features and enforce proper access control.

Read more
WordPressJune 25, 20263 min read

Introduction to Taxonomies: Organizing WordPress Content

Learn to register custom taxonomies to categorize your WordPress Knowledge Base. Discover how to enable hierarchical features and link them to your CPT.

Read more
4

Defining the Plugin Core Class

4 min
  • 5

    Understanding WordPress Hooks

    4 min
  • 6

    Implementing Custom Action Hooks

    4 min
  • 7

    Managing Hook Priorities

    3 min
  • 8

    Creating Admin Menus

    3 min
  • 9

    The Controller Layer for Admin Pages

    3 min
  • 10

    Registering Custom Post Types

    3 min
  • 11

    Configuring CPT Arguments

    3 min
  • 12

    Introduction to Taxonomies

    3 min
  • 13

    Designing Meta-Boxes

    3 min
  • 14

    Sanitizing User Input

    4 min
  • 15

    Saving Meta Data

    3 min
  • 16

    Database Basics with wpdb

    3 min
  • 17

    Secure CRUD Operations

    3 min
  • 18

    Querying with WP_Query

    3 min
  • 19

    Optimizing Queries

    3 min
  • 20

    The Model Layer for Data

    3 min
  • 21

    Enqueuing Scripts and Styles

    3 min
  • 22

    Plugin Template Hierarchy

    3 min
  • 23

    Creating Frontend Templates

    3 min
  • 24

    Building Shortcodes

    3 min
  • 25

    Advanced Shortcode Logic

    3 min
  • 26

    Introduction to Gutenberg Blocks

    3 min
  • 27

    The Settings API

    3 min
  • 28

    Validating Settings

    3 min
  • 29

    Implementing Nonces

    3 min
  • 30

    Capability Checks

    3 min
  • 31

    Handling Plugin Updates

    3 min
  • 32

    Internationalization (i18n)

    3 min
  • 33

    Debugging WordPress Plugins

    4 min
  • 34

    Unit Testing Foundations

    3 min
  • 35

    Handling AJAX Requests

    3 min
  • 36

    REST API Integration

    3 min
  • 37

    Advanced Database Queries

    3 min
  • 38

    Caching Strategies

    3 min
  • 39

    Plugin Security Best Practices

    Coming soon
  • 40

    Composer for Dependencies

    Coming soon
  • 41

    Theme Integration Hooks

    Coming soon
  • 42

    Managing Assets with Gulp/Webpack

    Coming soon
  • 43

    Documentation Standards

    Coming soon
  • 44

    Plugin Deployment Strategy

    Coming soon
  • 45

    Advanced MVC: Dependency Injection

    Coming soon
  • 46

    Handling Large Datasets

    Coming soon
  • 47

    Error Handling and Logging

    Coming soon
  • View full course