Custom Taxonomies
config/custom-taxonomies.php
This file contains all registration for custom taxonomies.
Registering a custom taxonomy
Section titled “Registering a custom taxonomy”Use TaxonomyBuilder::make() to register a custom taxonomy. The slug passed to make() is the taxonomy name registered with WordPress.
<?php
use App\Lib\WordPress\TaxonomyBuilder;
return [ TaxonomyBuilder::make('type') ->addTo('post'),];Labels are auto-derived from the slug (type → Type / Types). Override them with named():
return [ TaxonomyBuilder::make('type') ->named('Type', 'Types') ->addTo('post'),];With constants
Section titled “With constants”Define slugs in PostTypeConstant and TaxonomyConstant so they can be referenced without string literals:
final class TaxonomyConstant{ const TYPE = 'type';}<?php
use App\Constant\PostTypeConstant;use App\Constant\TaxonomyConstant;use App\Lib\WordPress\TaxonomyBuilder;
return [ TaxonomyBuilder::make(TaxonomyConstant::TYPE) ->named('Type', 'Types') ->addTo(PostTypeConstant::PORTFOLIO),];Builder methods
Section titled “Builder methods”| Method | Description |
|---|---|
named(string $singular, ?string $plural) | Set display labels. Auto-derived from slug if omitted. |
addTo(string $postTypeSlug) | Attach taxonomy to a post type. Call multiple times for multiple post types. |
setPrivate() | Hide from public queries and nav menus. |
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\Constant\TaxonomyConstant;use App\Lib\WordPress\TaxonomyBuilder;
return [ TaxonomyBuilder::make(TaxonomyConstant::GENRE) ->named('Genre', 'Genres') ->addTo(PostTypeConstant::EVENT),
TaxonomyBuilder::make(TaxonomyConstant::TYPE) ->named('Type', 'Types') ->addTo(PostTypeConstant::PORTFOLIO) ->addTo(PostTypeConstant::EVENT),];
