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') |
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']), ),],Knowledge Check
Test your understanding of this section
Loading questions...