concept-image / wp-make-cpt
Acorn commands that generate concept-image/wp-custom-post-types post types and taxonomies (classes, ACF field groups, options page, Blade views), publish ready-made presets and seed dummy content.
Requires
- php: >=8.4
- concept-image/php-file-editor: ^0.2
- concept-image/wp-archive-base: ^4.0
- concept-image/wp-custom-post-types: ^4.2
- concept-image/wp-make-block: ^3.4
- concept-image/wp-query: ^1.2
- fakerphp/faker: ^1.24
- log1x/acf-composer: ^3.4
- roots/acorn: ^6.4
Requires (Dev)
- brain/monkey: ^2.6
- concept-image/wp-hook-attributes: ^0.1
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
- pestphp/pest-plugin-arch: ^4.0|^5.0
- php-stubs/acf-pro-stubs: ^6.0
- szepeviktor/phpstan-wordpress: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Acorn commands that generate concept-image/wp-custom-post-types
post types and taxonomies in Bedrock + Acorn themes, part of the Concept Image stack:
make:cpt/make:tax: one command writes a post type's (or taxonomy's) class, ACF Composer field group, archive options page and Tailwind Blade views (the archive on wp-archive-base 4: composer, filters and results region views, AJAX action), then makes WordPress see it (registry clear, rewrite flush);cpt-preset: publishes ready-made post types: news, events, FAQ, team members, job offers, job applications, testimonies, locations;seed:cpt/seed:tax/seed:event: dummy content with Faker, ACF fields included.
Requirements
- PHP 8.4+ (wp-custom-post-types 4.2)
- Roots Acorn 6.4+,
log1x/acf-composer3.4+ (ACF Pro) concept-image/wp-custom-post-types4.2+ (it registers the generated classes, applies thearchiveQuery()of those implementingHasArchiveQueryto the archive's main query withconcept-image/wp-query1.2, and hides the block editor panel of the taxonomies edited through an ACF field)concept-image/wp-archive-base4.0+ (the archives:ArchiveData, the<x-archive.*>/<x-filter.*>components it publishes witharchive:install, the AJAX actionarchive:actionwrites; with wp-ajax 0.2)concept-image/php-file-editor0.1+ (installed with the package: the tokenizer-safe field group edits)- The generated views use the core components of
concept-image/wp-core-ui^0.2 (<x-core.section>,<x-core.container>,<x-core.heading>,<x-core.image>,<x-core.wysiwyg>:wp acorn core-ui:install) and wp-starter-theme's directives (@filled), its Tailwind theme, and its attribute-based hooks classes (ThemeServiceProvider::$hooks). - Optional:
concept-image/wp-make-block3+, to create the listing and hero blocks frommake:cpt.
Installation
In the theme, as a development dependency:
composer require --dev concept-image/wp-make-cpt
Creating a post type
Run the commands in the theme, inside the project container. They ask for what the options don't give; with -n
the defaults apply, so every command runs unattended.
wp acorn make:cpt case-study
wp acorn make:cpt case-study -n --singular="Étude de cas" --plural="Études de cas" --feminine
| Generated | Path | When |
|---|---|---|
Post type class (PostType, ShouldRegister; with an archive also HasArchiveQuery, $postsPerPage and archiveQuery()) | app/CustomPostTypes/CaseStudy.php | always |
ACF field group in acf-builder (thumbnail image, a taxonomy field per --taxonomies, the --fields fields) | app/Fields/PostType/CaseStudy.php | --acf has fields |
Archive options page (title, capability edit_others_posts) | app/Options/ArchiveCaseStudyOptions.php | --acf has options |
Archive view (<x-archive.archive> around the region views) | resources/views/archive-case-study.blade.php | --views has archive |
Its View Composer (ArchiveData::for(CaseStudy::class)->toArray()) | app/View/Composers/CaseStudyArchive.php | --views has archive |
| Its filters and results region views | resources/views/components/archives/case-study/{filters,results}.blade.php | --views has archive |
Its AJAX action (wp-archive-base archive:action, run by make:cpt) | app/Actions/Archives/FilterCaseStudy.php | --views has archive |
| Single view | resources/views/single-case-study.blade.php | --views has single |
| Card component and view | app/View/Components/Cards/CaseStudyCard.php, resources/views/components/cards/case-study-card.blade.php | --views has card |
Options:
- labels:
--lang=fr|en(French by default),--singular=,--plural=,--feminine/--no-feminine; - visibility:
--private(admin only),--archive/--no-archive,--rewrite/--no-rewrite,--slug=,--rest/--no-rest; - arguments:
--dashicon=,--hierarchical,--supports=title,editor,…,--taxonomies=,--position=,--delete-with-user; - files:
--views=archive,single,card(ornone),--acf=fields,options(ornone); - ACF fields:
--fields=intro,gallery,price_numberappends one acf-builder field per slug to the field group, its type and label inferred from the name by concept-image/wp-make-block's rules (*_image→ image,gallery→ gallery,*_number→ number,is_*→ true/false,items→ repeater, unknown → text; see its README). Slugs are snake_case, one the group already has (thumbnail) is skipped. Asked one by one in an interactive run, none with-n; ignored withoutfieldsin--acf; the--blockblocks get none; - blocks:
--block=fp,base,singlerunsmake:block(wp-make-block) for a front page listing, a page listing and a single hero; --force: overwrites existing files. Without it, the command stops when the class exists and skips any other file that exists (it says which ones).
Contradictory options (--private --archive, --views=archive --no-archive…) are rejected before anything is
written. Inside WP-CLI the command then clears wp-custom-post-types' registry and runs wp rewrite flush in a new
process, so the new archive and single URLs work at once; elsewhere it says to run wp rewrite flush.
The archive's query
A post type with an archive declares WordPress' main query of its archive on its class (wp-custom-post-types 4.2
applies it in pre_get_posts), and the archive view reads it instead of running a query:
use ConceptImage\WpCustomPostTypes\Interfaces\HasArchiveQuery;
use ConceptImage\WpQuery\PostQuery\Builder;
class CaseStudy extends PostType implements HasArchiveQuery, ShouldRegister
{
/**
* Posts per page of the archive (wp-custom-post-types applies it to the main query).
*/
protected static int $postsPerPage = 12;
// …
public static function archiveQuery(Builder $query): Builder
{
return $query
->filters([
// wp-query filters (…), each shown by an <x-filter.*> of resources/views/components/archives/case-study/filters.blade.php
TaxonomyFilter::make(CaseStudyCat::class)->archive(), // added by make:tax case-study-cat
])
->withAcf(['thumbnail']) // when the card and the field group are generated
->withTerms('case-study-cat'); // --taxonomies
}
}
// app/View/Composers/CaseStudyArchive.php ($views = ['archive-case-study'])
return ArchiveData::for(CaseStudy::class)->toArray(); // items, total, title, formUrl, resetUrl, scope, isFiltered, postType
{{-- resources/views/archive-case-study.blade.php --}}
<x-core.heading :level="1">{{ $title }}</x-core.heading>
<x-archive.archive :action="\App\Actions\Archives\FilterCaseStudy::class">
<x-archives.case-study.filters :items="$items" :total="$total" :is-filtered="$isFiltered" :reset-url="$resetUrl" />
<x-archives.case-study.results :items="$items" />
</x-archive.archive>
The archive is concept-image/wp-archive-base 4's: the region views (<x-archive.filters> with commented examples,
<x-archive.results> with the cards, count, empty state and pagination) are rendered by the template and by the AJAX
action alike, with the composer's data; the title is the options page's archive_case-study_title, else the label
(ArchiveData). No @php or query in the views. make:cpt writes them, then runs archive:action CaseStudy (the
action; the region views are kept) and archive:install archive.archive archive.filters archive.results; without
wp-archive-base 4 it prints both commands. A filter is a wp-query filter in archiveQuery()'s ->filters([…]) plus its
<x-filter.*> in the filters region view (wp-archive-base's documentation).
implements HasArchiveQuery opts the archive in (without it the archive stays WordPress' own, whatever
$postsPerPage says). The posts per page is protected static int $postsPerPage (keep the int type): wp-custom-post-types sets it on the
main query before archiveQuery(), which calls perPage() only to override it (without $postsPerPage, wp-query's
per_page config applies). archiveQuery() never calls published() or paged() (the request's status and page
apply), and eager-loads what the cards read (no query per card). A post type without an archive
(--no-archive, --private) gets no interface and neither member, and no archive views.
Creating a taxonomy
wp acorn make:tax case-study-category --object-type=case-study
wp acorn make:tax project-type -n --object-type=project,case-study --hierarchical --views=taxonomy --acf=fields
It writes app/Taxonomies/ProjectType.php and, on demand, app/Fields/Taxonomy/ProjectType.php (--acf=fields) and
resources/views/taxonomy-project-type.blade.php (--views=taxonomy). --object-type (comma-separated post types)
is required.
By default the terms are edited from an ACF taxonomy field on the post type's field group: meta_box_cb is false,
and concept-image/wp-custom-post-types 4.1 hides the block editor panel of such taxonomies (no hooks class to write
or register). make:tax inserts the field into app/Fields/PostType/{Studly}.php of each --object-type (an acf-builder
group with a single return $group->build();), at the end of its add*() chain, and prints it for the post types
it couldn't edit:
->addTaxonomy('project_type', [
'label' => 'Type de projet',
'taxonomy' => 'project-type',
'field_type' => 'radio', // radio: one term, checkbox: several
'required' => true,
'allow_null' => false,
'add_term' => false,
'save_terms' => true,
'load_terms' => true,
'return_format' => 'id',
])
A public taxonomy of a post type with an archiveQuery() becomes a filter of that archive (the first such
--object-type): the taxonomy implements HasArchiveQuery (ARCHIVE_POST_TYPES = [CaseStudy::class], return
CaseStudy::archiveQuery($query)->perPage(CaseStudy::postsPerPage());), make:tax appends
TaxonomyFilter::make(ProjectType::class)->archive() to the post type's ->filters([…]) and
<x-filter.links name="project-type" … /> to its filters region view (each printed when the file can't be edited,
kept when already there), then runs archive:install filter.links; --views=taxonomy writes the term view as
@include('archive-case-study'). Otherwise the term archive stays WordPress' own and the term view lists it alone.
One term per post is a radio; --multiple writes a checkbox labelled with the plural. --no-acf-field leaves the
field groups alone. --meta-box keeps WordPress' box instead (meta_box_cb null, no field).
make:cpt --taxonomies=project-type writes the same field in the new field group (WordPress' own taxonomies, such as
category, keep their box).
Other options: --lang, --singular, --plural, --feminine, --private, --hierarchical, --slug,
--rewrite / --no-rewrite, --rewrite-hierarchical, --rest / --no-rest, --nav-menus / --no-nav-menus,
--acf-field / --no-acf-field, --multiple, --force.
ACF fields are always written with acf-builder. A many-to-many link between two post types is a bidirectional ACF
relationship you add by hand on both field groups (see the wp-starter-theme skill's references/acf-fields.md).
Naming
The name is the WordPress key, singular kebab-case: case_study, CaseStudy and Case study give case-study
(class CaseStudy, views archive-case-study, ACF group case_study, options prefix archive_case-study_, the one
wp-archive-base's ArchiveData reads the title from). Accents are dropped (Événements → evenements).
Rejected: a leading digit, other characters than letters, digits, spaces, - and _, more than 20 characters (post
type) or 32 (taxonomy), WordPress reserved terms and query vars (type, name, year, s, p, paged,
author, order…), core post types and taxonomies, the wp- prefix and PHP reserved words as class names (list).
Labels
French labels elide their articles before a vowel or a silent h (« Modifier l'événement », « Ajouter un nouvel
événement »), agree with --feminine (« Aucune actualité trouvée ») and keep the noun lowercase mid-sentence (acronyms
excepted: « Modifier la FAQ »). --lang=en writes English labels. Every label is wrapped in __(…, 'ci-replace').
Presets
wp acorn cpt-preset # choose
wp acorn cpt-preset faq
wp acorn cpt-preset job-offer --force
| Preset | Post type, taxonomies | Ships |
|---|---|---|
news | news | intro and date fields; archive, single and card |
event | event, event-cat | dates, place and address; archive of upcoming events with category links and a date range, single, card |
faq | faq, faq-theme | WYSIWYG answer; archive with an Alpine.js accordion, theme links and search |
team-member | team-member | photo, role, bio, LinkedIn; back end only, no public page |
job-offer | job-offer, job-offer-type, job-offer-place, job-offer-function | contract details, dates, salary, description; back end, make:cpt's archive and single |
job-application | job-application (+ the job-offer taxonomies) | applicant data, link to the offer; admin only |
testimony | testimony | quote, author, role, company, photo; back end only, no public page |
location | location | address, contact, opening hours, coordinates, photo; back end, make:cpt's archive and single (no map) |
"Back end" is the classes, the ACF field groups and an options page for the presets with an archive; their bare
archive and single views are rendered from make:cpt's stubs (the theme's overrides included). The presets with an
archive (news, event, faq, job-offer, location) implement HasArchiveQuery and declare $postsPerPage and archiveQuery() like make:cpt (upcoming events
by start date, every FAQ question on one page: -1), event-cat and faq-theme implement it and reuse them (query and per page) on their term archives.
Their archives are make:cpt's (composer {Class}Archive, region views); news, event and faq ship theirs with their
filters (event: category links and a date range on the start date; faq: theme links and a search). cpt-preset then
runs archive:action (never forced) and archive:install for the components the views use, or prints them. Existing files are kept unless --force; the taxonomies job-offer and job-application share are always
kept.
The presets' terms are edited through ACF taxonomy fields too (meta_box_cb false, the block editor panel hidden by
wp-custom-post-types 4.1): a required radio for the event category and the job
offer's contract type, place and function, optional checkboxes for the FAQ themes, optional radios on a job
application. A job application's offer stays a one-way post object: a bidirectional relationship would show the
applicants' personal data in the job offers' field group.
Job applications hold personal data. The post type is admin only (no URL, REST, search, menus or sitemap) and has
its own capabilities, which app/Hooks/JobApplicationHooks.php grants to editors and administrators (register it in
ThemeServiceProvider::$hooks). Collect the applications with Gravity Forms (its uploads are protected by
wp-fortress), never put a CV in the public media library, define a retention period and register personal data
erasers and exporters.
Locations ship no map: it needs a JavaScript library outside the allowed ones (three, motion, embla-carousel); ask before adding one.
Dummy content
wp acorn seed:tax event-cat 4 # {name} {count=4}
wp acorn seed:cpt event 12 # {name} {count=24}
wp acorn seed:event 12 # {count=24}, same as seed:cpt event
seed:cpt fills the title, the content (when the post type supports the editor), a random term of each taxonomy
that has terms, and the ACF fields of the post type's acf-composer field group (else of every field group located on
it): dates as ACF stores them, start dates before end dates, choices by key, images from the media library. Use it on
development databases only.
Overriding the stubs
Copy a stub into the theme's stubs/wp-make-cpt/ folder and edit it: custom-post-type.stub, taxonomy.stub,
post-type-fields.stub, taxonomy-fields.stub, archive-options.stub, archive.stub, archive-composer.stub,
archive-filters.stub, archive-results.stub, single.stub, card-component.stub, card.stub, taxonomy-view.stub
(the term view including the archive), taxonomy-view-bare.stub (a term view without archived post type),
taxonomy-field.stub (the
->addTaxonomy() fragment: {{ field }}, {{ label }}, {{ taxonomy }}, {{ field_type }}, {{ required }},
{{ allow_null }}). The overrides also apply to the bare views of the presets. Placeholders: {{ className }}, {{ name }}, {{ studly }}, {{ snake }},
{{ camel }}, {{ singular }}, {{ plural }}, every label key ({{ edit_item }}, {{ update_item }}…), the
arguments ({{ public }}, {{ publicly_queryable }}, {{ exclude_from_search }}, {{ show_in_nav_menus }},
{{ show_in_rest }}, {{ position }}, {{ dashicon }}, {{ supports }}, {{ taxonomies }}, {{ has_archive }},
{{ hierarchical }}, {{ rewrite }}, {{ delete_with_user }}, {{ object_type }}, {{ meta_box_cb }}), the
options page ({{ options_prefix }}, {{ options_slug }}, {{ options_title }}, {{ update_button }},
{{ updated_message }}, {{ title_field }}), the field group and card texts ({{ thumbnail_field }},
{{ thumbnail_instructions }}, {{ more }}), the taxonomy fields of post-type-fields.stub ({{ taxonomy_fields }},
at the end of the last add*() call), {{ card_view }}, the results loop item {{ item }} ($item), the archive
names ({{ archive_view }}, {{ action_class }}, {{ filters_tag }}, {{ results_tag }}, and in taxonomy.stub
{{ archive_type }}, {{ archive_post_types }}, {{ object_type_doc }}), and the archive
query of custom-post-type.stub ({{ archive_interface_import }}, {{ archive_imports }}, {{ per_page }},
{{ archive_query }}, each alone on its line: a line whose placeholder renders empty is removed; and
{{ archive_interface }}, HasArchiveQuery, between implements and ShouldRegister).
Claude Code
The package ships a wp-make-cpt skill and Claude Code rules (an always-loaded rule, and one for
stubs/wp-make-cpt/). Publish them at the Bedrock root with wp acorn vendor:publish --tag=wp-make-cpt-ai
(wp-make-cpt-skill or wp-make-cpt-rules for one of them); wp-starter-theme's wp acorn starter:ai includes them.
Upgrading
See UPGRADE.md (3.x → 4.0: wp-archive-base 4 archives; 3.1 → 3.2: the archive query on the class; 2.x → 3.0: options, stub folder, presets).
Development
composer ci # Pint, Larastan (max on src/, 5 on presets/), Pest
Until concept-image/wp-archive-base 4.0, concept-image/wp-query 1.2 and concept-image/wp-ajax 0.2 are released,
composer.json resolves them from path repositories (../wp-archive-base, ../wp-query, ../wp-ajax, sibling
checkouts, mirrored), so run Composer with the parent directory mounted
(docker run --rm -u "$(id -u):$(id -g)" -v "$PWD/..":/work -w /work/wp-make-cpt composer:2 composer ci). Remove the
path repositories once they are published.