Skip to content

Custom Post Types

config/custom-post-types.php

This file contains all registration for custom post types.

Use PostTypeBuilder::make() to register a custom post type. The slug passed to make() is the post type name registered with WordPress.

config/custom-post-types.php
<?php
use App\Lib\WordPress\PostTypeBuilder;
return [
PostTypeBuilder::make('portfolio'),
];

Labels are auto-derived from the slug (portfolioPortfolio / Portfolios). Override them with named():

return [
PostTypeBuilder::make('portfolio')
->named('Portfolio Item', 'Portfolio Items'),
];

Define the slug once in PostTypeConstant and reference it everywhere — registration, queries, ACF locations, and class maps:

app/Constant/PostTypeConstant.php
final class PostTypeConstant
{
const PORTFOLIO = 'portfolio';
}
config/custom-post-types.php
<?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,
]);
MethodDescription
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.
<?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'),
];