Post Type Fields
The Post_Type_Fields class registers ACF field groups and attaches them to specific post types, page templates, or taxonomy term edit screens. Fields are composed with ACF_Builder islands, and show_when is automatically resolved.
This is used when you need custom fields on the edit screen of a post type (not inside Flexible Content modules).
Key Features
Section titled βKey Featuresβ- Island-composed fields: Uses
ACF_Builderfor field definitions - Multiple location types: Attach to post types, page templates, or taxonomy term screens
- Combined conditions: Support for
post_taxonomyconditions (e.g., show fields only for a specific term) - Deterministic keys: Safe for environment migrations
- show_when support: Conditional visibility between fields
Configuration File
Section titled βConfiguration Fileβfunctions/project/config/post-type-fields_config.php
<?phpreturn [ [ 'title' => 'Resource Details', 'post_type' => 'resource', 'fields' => array_merge( ACF_Builder::image(['name' => 'hero_image', 'label' => 'Hero Image']), ACF_Builder::text(['name' => 'hero_subtitle', 'label' => 'Hero Subtitle']), ACF_Builder::boolean(['name' => 'is_gated', 'label' => 'Is Gated Content?']), ACF_Builder::url([ 'name' => 'gate_url', 'label' => 'Gate URL', 'show_when' => ['is_gated', '==', '1'], ]), ), ], [ 'title' => 'Team Member Details', 'post_type' => 'team', 'fields' => array_merge( ACF_Builder::title(['name' => 'position', 'label' => 'Position/Role']), ACF_Builder::url(['name' => 'linkedin', 'label' => 'LinkedIn URL']), ACF_Builder::image(['name' => 'headshot', 'label' => 'Headshot']), ), ],];Parameters
Section titled βParametersβEach group in the array accepts:
| Parameter | Type | Description |
|---|---|---|
title | string | Field group title (shown in the admin) |
post_type | string or array | Post type(s) to attach to |
page_template | string or array | Page template(s) to attach to |
taxonomy | string or array | Taxonomy term edit screen(s) |
post_taxonomy | string or array | Taxonomy condition (e.g., 'type:case-study') |
page_type | string | Page type condition: parent, child, top_level, front_page. AND with other rules. Single value only. |
fields | array | Fields composed with ACF_Builder |
01. Attach to a Post Type
Section titled β01. Attach to a Post Typeβ<?php[ 'title' => 'Industry Info', 'post_type' => 'industry', 'fields' => array_merge( ACF_Builder::image(['name' => 'icon', 'label' => 'Industry Icon']), ACF_Builder::wysiwyg(['name' => 'overview', 'label' => 'Overview']), ),],02. Attach to Multiple Post Types
Section titled β02. Attach to Multiple Post Typesβ<?php[ 'title' => 'SEO Fields', 'post_type' => ['resource', 'blog', 'industry'], 'fields' => array_merge( ACF_Builder::text(['name' => 'seo_title', 'label' => 'SEO Title']), ACF_Builder::text(['name' => 'seo_description', 'label' => 'SEO Description', 'rows' => 2]), ),],03. Attach to a Page Template
Section titled β03. Attach to a Page Templateβ<?php[ 'title' => 'Homepage Fields', 'page_template' => 'page-home.php', 'fields' => array_merge( ACF_Builder::title(['name' => 'hero_heading']), ACF_Builder::text(['name' => 'hero_description']), ),],04. Attach to a Taxonomy Term Edit Screen
Section titled β04. Attach to a Taxonomy Term Edit ScreenβShow fields when editing a taxonomy term (e.g., category edit page):
<?php[ 'title' => 'Category Settings', 'taxonomy' => 'resources_categories', 'fields' => array_merge( ACF_Builder::image(['name' => 'category_icon', 'label' => 'Icon']), ACF_Builder::text(['name' => 'category_description', 'label' => 'Short Description']), ),],05. Conditional by Taxonomy Term
Section titled β05. Conditional by Taxonomy TermβShow fields only when a post has a specific taxonomy term assigned:
<?php[ 'title' => 'Case Study Fields', 'post_type' => 'resource', 'post_taxonomy' => 'resource_type:case-study', 'fields' => array_merge( ACF_Builder::title(['name' => 'client_name', 'label' => 'Client Name']), ACF_Builder::text(['name' => 'results', 'label' => 'Key Results']), ),],06. Conditional by Page Type
Section titled β06. Conditional by Page TypeβShow fields only on pages that match a hierarchy condition (e.g. parent pages only):
<?php[ 'title' => 'Parent Page Fields', 'post_type' => 'page', 'page_type' => 'parent', 'fields' => array_merge( ACF_Builder::text(['name' => 'section_intro', 'label' => 'Section Intro']), ),],Accepted values: parent, child, top_level, front_page. AND with post_type / page_template. Single value only.
Knowledge Check
Test your understanding of this section
Loading questions...