How to Register Custom Post Types with register_post_type

Contents

How to Register Custom Post Types with register_post_type

WordPress custom post types (CPTs) allow developers to extend the CMS beyond posts and pages. The register_post_type() function is the core API for creating new content types. This article provides a serious, detailed and extensive guide on its usage, covering arguments, labels, capabilities, rewrite rules, and practical tips.

Why Use Custom Post Types

By default, WordPress supports post and page content types. CPTs enable you to organize and display unique content—such as portfolios, testimonials or products—in a structured way:

  • Separation of concerns: Keep different content logically separated.
  • Custom archives: Automatic archive pages without custom queries.
  • Enhanced UX: Tailor editing screens to specific fields and features.
  • SEO benefits: Clean URLs and semantic structure.

Basic Code Example

add_action(init, myplugin_register_portfolio)

function myplugin_register_portfolio() {
  labels = array(
    name               => Portfolios,
    singular_name      => Portfolio,
    menu_name          => Portfolio,
    name_admin_bar     => Portfolio Item,
    add_new            => Add New,
    add_new_item       => Add New Portfolio,
    new_item           => New Portfolio,
    edit_item          => Edit Portfolio,
    view_item          => View Portfolio,
    all_items          => All Portfolios,
    search_items       => Search Portfolios,
    not_found          => No portfolios found.,
    not_found_in_trash => No portfolios found in Trash.
  )

  args = array(
    labels             => labels,
    public             => true,
    has_archive        => true,
    rewrite            => array(slug => portfolio),
    supports           => array(title,editor,thumbnail),
    menu_icon          => dashicons-portfolio,
  )

  register_post_type(portfolio, args)
}

Detailed Breakdown of Arguments

The args array controls how your CPT behaves and appears. Below is a table of the most common parameters:

Argument Type Description Default
labels array Human-readable labels.
public bool Whether it’s publicly queryable. false
has_archive bool Generates archive page. false
rewrite arraybool Permalink settings. true
supports array Features to enable. title, editor
menu_icon string Admin menu icon. dashicons-admin-post

Comprehensive Labels Reference

The labels array lets you tailor the UI text. Recommended keys include:

  • name: General name, plural.
  • singular_name: Name for one object.
  • menu_name: Text in admin menu.
  • name_admin_bar: Toolbar name.
  • add_new / add_new_item: Buttons for creation.
  • edit_item, new_item, view_item
  • search_items, not_found, not_found_in_trash

See the official documentation for a full list:
register_post_type()

Custom Rewrite Slugs and Permalinks

The rewrite argument controls URL structure:

  • slug: Base URL segment. Eg. products.
  • with_front: Whether to prepend front base.
  • feeds, pages: Enable pagination feeds.
rewrite =gt array(
  slug       =gt portfolio,
  with_front =gt false,
  feeds      =gt true,
  pages      =gt true
)

After registering, remember to flush permalinks by visiting Settings → Permalinks or calling flush_rewrite_rules().

Advanced Capabilities and Permissions

By default, CPTs inherit post capabilities. For granular control, use the capability_type and capabilities arguments:

capability_type => array(portfolio,portfolios),
map_meta_cap    => true,
capabilities    => array(
  edit_post           => edit_portfolio,
  read_post           => read_portfolio,
  delete_post         => delete_portfolio,
  edit_posts          => edit_portfolios,
  edit_others_posts   => edit_others_portfolios,
  publish_posts       => publish_portfolios,
  read_private_posts  => read_private_portfolios
)

Ensure to add these capabilities to roles via add_role() or add_cap().

Hierarchical vs. Non-Hierarchical

The hierarchical flag determines parent/child relationships:

  • true: Behaves like pages (supports nesting).
  • false: Behaves like posts (flat).

Tips for Development and Deployment

  1. Wrap registrations in an init callback with proper priority.
  2. Use constants or filters for slugs to allow customization.
  3. Separate labels into their own function for translatability.
  4. Flush rewrite rules only upon activation/deactivation of plugins to avoid performance issues.

Useful Plugins Tools

Conclusion

Registering custom post types with register_post_type() is a foundational skill for WordPress developers. With clear labels, appropriate capabilities, and well-defined rewrite rules, you can create powerful content experiences. For full details, consult the official reference at the WordPress Developer Resources:
https://developer.wordpress.org/reference/functions/register_post_type/.



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

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