Skip to main content

benecaster_field_groups

Filter Free

Filters the array of field group definitions before they are returned for a given custom post type. Fires when the episode editor (or any registered CPT editor) loads field values for display — not on the Field Group list screen.

Use to inject dynamically generated groups, reorder groups, or hide groups for specific CPTs. Synthetic groups with non-integer IDs are display-only: they appear in the editor with their injected fields, but write operations reject non-numeric field IDs and silently drop submitted values. To persist values from synthetic groups, add a dedicated REST endpoint from your add-on.

Removing a group clears its fields on the next save. Hiding a real (numeric-ID) group hides it at load time, but its fields are absent from the save payload when the editor next submits. The save endpoint treats absent fields as empty writes — any previously stored value is overwritten with an empty string. This side-effect is silent: the user sees no error, and the data loss only becomes apparent after the save completes. Only remove a group when you also intend to clear its field values, or when you are certain the post will not be saved through the editor while the filter is active.

Parameters

Name Type Default Description
$groups array Array of field group objects {id, name, cpt_slug, display_order, field_count, fields}
$cpt_slug string The CPT being queried (e.g. 'benecaster_episode', 'benecaster_guest')

Returns: array

Examples

Inject synthetic read-only group for episodes

add_filter( 'benecaster_field_groups', function( $groups, $cpt_slug ) {
    // Inject a synthetic "AI Summary" group for episodes only.
    if ( 'benecaster_episode' === $cpt_slug ) {
        $groups[] = [
            'id'            => 'synthetic-ai-summary',
            'name'          => 'AI Summary',
            'cpt_slug'      => $cpt_slug,
            'display_order' => 99,
            'field_count'   => 1,
            'fields'        => [],
        ];
    }
    return $groups;
}, 10, 2 );

Add add-on field group when feature is active

add_filter( 'benecaster_field_groups', function ( array $groups, string $cpt_slug ): array {
    if ( 'benecaster_episode' !== $cpt_slug || ! my_addon_feature_enabled() ) {
        return $groups;
    }

    $groups[] = [
        'id'            => 'my_addon_dynamic_group',
        'name'          => 'Sponsor Information',
        'cpt_slug'      => 'benecaster_episode',
        'display_order' => 99,
        'fields'        => [
            [
                'id'            => 'my_addon_field_sponsor_name',
                'group_id'      => 'my_addon_dynamic_group',
                'field_label'   => 'Sponsor Name',
                'field_key'     => 'sponsor_name',
                'field_type'    => 'text',
                'display_order' => 0,
                'options'       => null,
                'required'      => false,
            ],
        ],
    ];

    return $groups;
}, 10, 2 );

Notes

This filter does not fire on the Field Group list screen — only when field values are being loaded for an object. Removing a group via this filter hides it from the editor UI but does not delete stored values; existing values remain in the database and are still returned by benecaster_get_field() and benecaster_get_fields(). Synthetic groups with non-integer IDs are display-only — write operations reject non-numeric field IDs and silently drop submitted values.

Need this built rather than just documented? See our services →