Custom Post Types
config/custom-post-types.php
This file contains all registration for custom post types.
Registering a custom post type
Section titled “Registering a custom post type”Use PostTypeBuilder::make() to register a custom post type. The slug passed to make() is the post type name registered with WordPress.
<?php
use App\Lib\WordPress\PostTypeBuilder;
return [ PostTypeBuilder::make('portfolio'),];Labels are auto-derived from the slug (portfolio → Portfolio / Portfolios). Override them with named():
return [ PostTypeBuilder::make('portfolio') ->named('Portfolio Item', 'Portfolio Items'),];With constants
Section titled “With constants”Define the slug once in PostTypeConstant and reference it everywhere — registration, queries, ACF locations, and class maps:
final class PostTypeConstant{ const PORTFOLIO = 'portfolio';}<?php
use App\Constant\PostTypeConstant;use App\Lib\WordPress\PostTypeBuilder;
return [ PostTypeBuilder::make(PostTypeConstant::PORTFOLIO) ->named('Portfolio'),];The same constant is then used when querying posts with Timber:
use App\Constant\PostTypeConstant;use Timber\Timber;
$posts = Timber::get_posts([ 'post_type' => PostTypeConstant::PORTFOLIO, 'posts_per_page' => 12,]);Builder methods
Section titled “Builder methods”| Method | Description |
|---|---|
named(string $singular, ?string $plural) | Set display labels. Auto-derived from slug if omitted. |
hasArchive(bool $enable) | Enable or disable the archive page. |
setPrivate() | Hide from public queries, search, and frontend URLs. |
rewriteSlug(string $slug) | Override the URL slug. |
setMenuIcon(string $icon) | Set a Dashicon for the admin menu. |
setCapabilityType(string $singular, string $plural) | Set custom capabilities. |
setLabel(string $key, mixed $value) | Override a single label. |
setLabels(array $labels) | Override multiple labels. |
setArg(string $key, mixed $value) | Override a single WP arg. |
setArgs(array $args) | Override multiple WP args. |
Example
Section titled “Example”<?php
use App\Constant\PostTypeConstant;use App\Lib\WordPress\PostTypeBuilder;
return [ PostTypeBuilder::make(PostTypeConstant::PORTFOLIO) ->named('Portfolio') ->hasArchive(true) ->setMenuIcon('dashicons-portfolio'),
PostTypeBuilder::make(PostTypeConstant::EVENT) ->named('Event', 'Events') ->hasArchive(true) ->rewriteSlug('events') ->setMenuIcon('dashicons-calendar-alt'),];
