Skip to content

Custom Post Type (CPT)

1. Using CLI

General Command:
sh
php wp cxf-make:post-type {post_type_name}
  • {post_type_name} will be your post type name.
  • To create multiple post types, separate them with spaces.
sh
php wp cxf-make:post-type {post_type_name} {post_type_name}
Naming Conventions:
  • UpperCamelCase: PostTypeName (Recommended)
  • camelCase: postTypeName
  • snake_case: post_type_name
  • kebab-case: post-type-name
  • Mixed Case: postType_Name or post-type_name or Post_typeName

INFO

All formats are converted to UpperCamelCase to follow the PSR-4 standard.

IMPORTANT

You cannot use PostType as a name since it is a reserved word in the framework. Check reserved words here.

Example:
sh
php wp cxf-make:post-type CustomMenu

This command will generate all necessary files and directories to register the custom post type.

Expected CLI Response:
Success: The post_type custom-menu's post_type file has been created at ...
Generated Files and Directories:
  • Post Type File: /codexshaper-framework/src/PostTypes/CustomMenu.php

2. Using Option Builder

The framework includes a powerful option builder to easily create post types.

php
use CodexShaper\Framework\Builder\CustomPostType;

CustomPostType::create(
    'custom_post', // Post Type ID
    array(
        "post_type"=> "custom_post",
        "name"=> "Custom posts",
        "singular_name"=> "Custom post",
        "plural_name"=> "Custom posts",
        "supports" => array(
            "title",
            "editor",
            "author",
            "excerpt",
        ),
        "rewrite" => array(
            "slug" => "custom-post",
            "with_front" => false,
        ),
        "exclude_from_search" => true,
        "has_archive" => true,
        "publicly_queryable" => true,
        "hierarchical" => false,
        "show_in_menu" => true,
        "show_ui" => true,
        "show_in_nav_menus" => true,
        "menu_icon" => false,
    )
);
ArgumentTypeDefaultDescription
post_typestring-Unique slug/ID for the custom post type.
namestring-Plural label (e.g., "Books").
singular_namestring-Singular label (e.g., "Book").
plural_namestring-Another way to specify plural form.
supportsarraytitle, editorFeatures like title, editor, author, thumbnail, comment, excerpt etc.
rewritearray|booltrueControls permalinks (slug, with_front).
exclude_from_searchboolfalseExcludes from search results if true.
has_archiveboolfalseEnables archive at /custom-dynamic-post/.
publicly_queryablebooltrueAllows front-end queries.
hierarchicalboolfalseIf true, behaves like pages (parent-child structure).
show_in_menubooltrueShows in admin menu or as submenu if string provided.
show_uibooltrueDisplays in admin UI if true.
show_in_nav_menusbooltrueAvailable in navigation menus.
menu_iconstring|boolfalseFontawesome class or custom icon URL.

After executing this code, the Portfolios menu will appear in the WordPress dashboard.

3. Create from Dashboard

Navigate to CodexShaper Framework >> Custom Post Types.

Custom Post Type View

ArgumentTypeDefaultDescription
Post Typestring-Unique slug/ID for the custom post type.
Singular Namestring-Singular label (e.g., "Book").
Plural Namestring-Plural label (e.g., "Books").
Front Slugstring-A slug that can be used for the front-end (optional).
Supportsarraytitle, editorFeatures like title, editor, author, thumbnail, comment, excerpt etc.
Exclude From SearchboolfalseExcludes from search results if true.
Has ArchiveboolfalseEnables archive at /custom-dynamic-post/.
Publicly QueryablebooltrueAllows front-end queries.
hierarchicalboolfalseIf true, behaves like pages (parent-child structure).
Show in UIbooltrueDisplays in admin UI if true.
Show in Admin MenubooltrueShows in admin menu or as submenu if string provided.
Nav Iconstring|boolfalseFontawesome class or custom icon URL.
Show in Nav MenuboolfalseToggle to show or hide in navigation menus.
Is ActivebooltrueSet this to Yes to activate the metabox.

Once saved, the new post type appears in the admin panel.

4. Creating Manually

Steps:
  1. Navigate to:
    sh
    {project_root}/wp-content/plugins/codexshaper-framework/src/PostTypes
  2. Create a file using UpperCamelCase.php naming convention (e.g., CustomMenu.php).
  3. Open the file in a text editor and insert the following code:
php
<?php

namespace CodexShaper\Framework\PostTypes;

use CodexShaper\Framework\Foundation\PostType;

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

class CustomMenu extends PostType {
    public function __construct() {
        parent::__construct();
    }

    public function get_name() {
        return 'custom-menu';
    }

    public function get_title() {
        return join( ' ', array_map( 'ucfirst', explode( '-', $this->post_type ) ) );
    }

    public function get_capability_type() {
        return esc_html__( 'post', 'textdomain' );
    }

    public function get_supports() {
        return array(
            'title',
            'editor',
            'thumbnail',
        );
    }

    public function is_public() {
        return true;
    }

    public function is_publicly_queryable() {
        return true;
    }

    public function is_show_ui() {
        return true;
    }

    public function is_show_in_rest() {
        return true;
    }

    public function is_query_var() {
        return true;
    }

    public function is_unregister() {
        return false;
    }
}

Done! Your custom post type is now registered.