Detailed explanation of custom post types in WordPress

3-minute read
2026-03-14
2026-06-04
2,706
I earn commissions when you shop through the links below, at no additional cost to you.

Detailed explanation of custom post types in WordPress

The default article types in WordPresspostand page typespageIt's sufficient to manage most content, but for complex websites that require structured display of specific content (such as products, portfolios, events, teams, etc.), its functionality becomes inadequate. The emergence of custom post types was precisely to address this core pain point, allowing developers to go beyond the scope of “articles” and “pages” to create dedicated back-end management, front-end display, and query logic for any type of content.

By customizing article types, you can add a separate publishing interface for “Products”, create dedicated profile entry fields for “Team Members”, and assign unique URL structures, categorization methods, and template files to these contents. This not only greatly enhances the clarity and efficiency of content management, but also serves as an indispensable technical foundation for building enterprise-level, highly customized WordPress websites.

Why do we need to customize article types?

By using custom article types, you can logically separate different types of content, which brings significant advantages in many aspects.

Recommended Reading In-depth Analysis of WooCommerce: A Complete Guide to Building an Efficient E-commerce Website from Scratch

Achieve a clear separation of content management

When your website includes blogs, product displays, news announcements, and success stories at the same time, if you all use the default “Article” to manage them, the backend will become extremely chaotic. Different types of content are mixed in the same list, making it extremely inconvenient to edit and search for them.

UltaHost WordPress Hosting
30-day refund guarantee, unlimited bandwidth and database usage, free DDoS protection; purchase for 3 years and get a discount of 50%.

After creating separate “Product” and “Case” types, administrators can see separate “Product” and “Case” menu items on the left side of the backend. After clicking to enter, their management interface is similar to the article management interface, but completely independent and non-interfering with each other. This separation makes the content organization well-organized and greatly reduces the management complexity.

Customize exclusive fields and metadata

The default article types mainly include common fields such as title, body, abstract, category, and tags. For “products”, you may need fields such as price, specifications, and inventory; for “events”, you will need start time, end time, and location, etc.

Custom article types can be combined with plugins or code such as Advanced Custom Fields to easily add dedicated metadata fields for specific content types. These fields are displayed in a form in the backend, and can be flexibly called through template tags in the frontend, enabling in-depth customization of content structures.

Create an independent URL structure and template system

Custom article types support the definition of your own URL aliases (Slugs). For example, you can set the fixed link for product types to/product/%postname%Set the activity to/event/%postname%This not only makes the URL more meaningful and SEO-friendly, but also helps users understand the structure of the website.

Recommended Reading Master WooCommerce custom page templates: A practical development guide from scratch to completion

More importantly, WordPress's template hierarchy system will prioritize searching for template files that match the custom post type. For example, for a post namedproductAccording to the type of the file, the system will search for it in turn.single-product.phpsingle.phpsingular.phpAnd so on. This allows you to design completely different front-end display styles for different types of content without having to write complex conditional judgment logic in a single template.

There are two methods to create custom article types

In WordPress, there are mainly two mainstream methods to create custom post types: registering them in the theme's function files through code, and using powerful dedicated plugins.

Create quickly with the help of plugins

For users who are not familiar with code development or need to launch a website quickly, using plugins is the most convenient approach. Custom Post Type UI and Toolset Types are two popular plugins. They provide an intuitive user interface that allows you to define all parameters of an article type, including its name, tags, icon, whether it's public, and whether it supports archive pages, by filling out a form.

hosting.com Shared Hosting
High performance with AMD EPYC CPUs, NVMe SSD storage and LiteSpeed, 24/7, 24x7 expert in-house support, advanced security measures including SSL, brute force, malware and DDoS protection, savings of up to 73%

The advantage of this method is that it doesn't require writing code, the settings take effect immediately, and it is usually well integrated with the meta-field management function provided by the plugin. However, its disadvantages are equally obvious: the functionality is limited by the options provided by the plugin, and the degree of customization may be insufficient; and if the theme is changed or the plugin is deactivated in the future, although the data will be preserved in the database, the registration function may be lost, and additional code is needed to re-declare it.

Register via a code function (recommended)

For developers who pursue stability, controllability, and performance, registering in themes or custom plugins through code is a more professional and recommended approach. The core of this method is to use the tools provided by WordPress. register_post_type() Function.

You need to place the registration code in the subject line of the email.functions.phpIn the file, or better yet, create a separate site function plugin. The advantage of doing this is that the function is separated from the theme, and even if the theme is changed, the custom article types and their data will still work flawlessly.

Recommended Reading Analyzing the Entire Process of Website Construction: A Practical Technical Guide to Building a Professional-Level Website from Scratch

Below is a basic code example for creating a custom article type called “Product”:

function create_product_post_type() {
    $labels = array(
        'name'               => '产品',
        'singular_name'      => '产品',
        'menu_name'          => '产品管理',
        'add_new'            => '添加新产品',
        'add_new_item'       => '添加新产品',
        'edit_item'          => '编辑产品',
        'new_item'           => '新产品',
        'view_item'          => '查看产品',
        'search_items'       => '搜索产品',
        'not_found'          => '未找到产品',
        'not_found_in_trash' => '回收站中无产品',
    );

    $args = array(
        'labels'              => $labels,
        'public'              => true, // 是否公开
        'has_archive'         => true, // 是否有归档页
        'menu_icon'           => 'dashicons-cart', // 后台菜单图标
        'menu_position'       => 5, // 菜单位置
        'supports'            => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'), // 支持的功能
        'rewrite'             => array('slug' => 'products'), // URL别名
        'show_in_rest'        => true, // 是否在古腾堡编辑器和REST API中启用
    );

    register_post_type('product', $args); // 注册,'product'为内部标识符
}
add_action('init', 'create_product_post_type'); // 在初始化时执行函数

This code uses the following methods to achieve the desired effect:register_post_type()The function registers a name calledproductIt specifies the type of the article and configures its display tags, basic functions, and supported modules. Add it tofunctions.phpAfter that, simply refresh the background and you'll be able to see the newly added “Product Management” menu.

InterServer Shared Hosting
Shared hosting $2.50 USD per month , first month $0.1 USD promo code tryinterserver, 461 cloud apps scripts, one click install.

Advanced configuration and template customization

Registrating the basic type is just the first step. To make the custom article type truly powerful and easy to use, we also need to configure it at an advanced level and create a dedicated template for it.

Configure the featured parameters and associated taxonomies

register_post_type()The function's$argsThe parameter array provides dozens of configuration items. Some of the key advanced options include:
* ‘publicly_queryable’Is it possible to access a single article through the front-end URL?
* ‘exclude_from_search’\n: Whether to exclude this type of article from the search results on the platform.
* ‘capability_type’Define the permission capabilities of this type, which enables fine-grained permission control.
* ‘taxonomies’Connect to an existing or custom taxonomy. For example, associate a custom “Product Category” with “Products”.”product_catAnd tagsproduct_tagYou can categorize products in the same way as managing article categories.

Create a custom taxonomy

utilization register_taxonomy() A function can create a dedicated category or tag for your custom article types. For example, create a “Product Series” category for “Products”:

function create_product_taxonomy() {
    register_taxonomy(
        'product_series', // 分类法标识符
        'product', // 绑定的文章类型
        array(
            'label' => '产品系列',
            'rewrite' => array('slug' => 'product-series'),
            'hierarchical' => true, // 是否为层级式(类似分类)
            'show_in_rest' => true,
        )
    );
}
add_action('init', 'create_product_taxonomy');

Develop dedicated template files

Template customization is the core of front-end display. According to the WordPress template hierarchy, you need to create corresponding template files for your theme. For example:
1. Archive page template: Createarchive-product.php\nCustomizedyoursite.com/products/This page is used to list all products.
2. Single article template: Createsingle-product.phpFrom the details page of a single product, such asyoursite.com/products/sample-product/
3. Taxonomy archiving template: Createtaxonomy-product_series.phpTo display a list of all products under a specific product series.

In these template files, you can use the standard WordPress loop, but you usually need to combine it with custom field queries to display product prices, specifications, and other specific information.

Use pre_get_posts to modify the main query

Sometimes, you may need to adjust the default query parameters on the product archive page, such as changing the number of items displayed per page or the sorting method. In this case, you should not directly modify the query in the template file, but instead, you should use the QuerySet API to achieve this. pre_get_posts This action hook is used to modify the main query, which is a best practice in WordPress development.

function modify_product_archive_query($query) {
    if (!is_admin() && $query->is_main_query() && is_post_type_archive('product')) {
        $query->set('posts_per_page', 12); // 每页显示12个产品
        $query->set('orderby', 'date'); // 按日期排序
        $query->set('order', 'DESC'); // 降序排列
    }
}
add_action('pre_get_posts', 'modify_product_archive_query');

summarize

Custom post types are one of the core features that helped WordPress evolve from a blogging system to a powerful content management system. They provide an elegant solution for managing and displaying complex content through logical separation, field customization, and template specialization. Whether it's quickly setting things up with plugins or through other methods, custom post types offer powerful functionality for content management and presentation.register_post_type()The function provides deep code control, allowing developers to find an implementation path that suits their project needs. By combining custom taxonomies, advanced fields, and theme development that follows the template hierarchy, you can build various professional websites with clear structures, powerful functionality, and excellent user experiences. Mastering and making good use of this function is an essential step for every WordPress developer to advance their skills.

FAQ Frequently Asked Questions

Where is the data for custom article types stored in the database?

All articles (content) of the custom article type are mainly stored inwp_postsIn the data table, itpost_typeThe value of the field is the identifier you defined when you registered (for example).productThe taxonomy information associated with these articles is stored inwp_term_relationshipsandwp_term_taxonomyIn the table. And the information added through custom fields (metadata) is stored inwp_postmetaIn the data table.

If I disable the plugin that creates custom article types, will the content be lost?

The content of the articles themselves is usually not lost, as they have already been stored inwp_postsThe issue is that the code for registering this article type (provided by the plugin) has become invalid, so WordPress will no longer recognize this type. This will result in the disappearance of the backend management menu, preventing you from viewing and managing this content in the backend. At the same time, the article links on the frontend may not be accessible normally. To prevent this situation, it is recommended to transfer the critical registration code to the child theme.functions.phpOr in the customized function plug-ins.

How to modify the fixed link structure of an existing custom article type?

To modify the structure of the permanent link, you need to update the information provided during registration.rewriteParameters. You can modify the parameters you set when you registered initially.register_post_type()In the function,‘rewrite’ => array(‘slug’ => ‘new-slug’)After making the changes, you must go to the “Settings” -> “Permalinks” page in the WordPress backend, without making any changes. Just click the “Save Changes” button. This operation will refresh the rewrite rules and enable the new link structure to take effect.

Can we create separate block editor templates for custom article types?

Yes, it's absolutely possible. Since the introduction of the Gutenberg editor and the support for Full Site Editing (FSE) in WordPress 5.0, you can create dedicated block templates or template parts for custom post types. This is usually more intuitive when using a block-themed theme that supports FSE, such as Twenty Twenty-Three. You can do this in the theme's settings.templatesCreate folders such as the following under the specified folder:single-product.htmlSuch block template files, or through the theme'stheme.jsonIn the files and registration parameters‘template’‘template_lock’Configure the options to define a unified block layout for all articles of this type.