benecaster_get_fields()
Returns all custom field values for a named field group on a given object.
Signature
benecaster_get_fields( string $group_key, int $object_id ): array
Parameters
| Name | Type | Description |
|---|---|---|
$group_key |
string | Field group name or numeric ID as a string. Accepts the group’s display name (e.g. 'Interview Info') or its database ID (e.g. '5'). |
$object_id |
int | The post ID of the object to read values for |
Return Value
array — associative array of { field_key => value } pairs. Each value passes through the benecaster_field_value filter before being returned. Returns an empty array if the group is not found or no values are saved for the object.
Notes
This is a Free function. It does not require a paid license.
When resolving by name, the lookup uses a MySQL WHERE name = %s query. On the default MySQL collation (utf8mb4_unicode_ci) this is case-insensitive — 'Interview Info' and 'interview info' resolve to the same group. Prefer using the group name in code so the ID can change (e.g. after reinstalling on a new site) without breaking the integration.
All defined fields appear in the returned array, including fields with no stored value for this object. Unset fields have a null value rather than being absent. Use $fields['my_key'] ?? null if you want a safe default, but the key will already be present.
Each value passes through the benecaster_field_value filter individually.
Example Usage
Read all values for an “Interview Info” field group and use them in a template:
$fields = benecaster_get_fields( 'Interview Info', $episode_id );
// Returns: [ 'guest_name' => 'Sarah Chen', 'booking_url' => 'https://calendly.com/sarahchen' ]
$guest_name = $fields['guest_name'] ?? null;
$booking_url = $fields['booking_url'] ?? null;
if ( $guest_name ) {
echo '<p>' . esc_html( $guest_name ) . '</p>';
}
Using the group ID instead of name:
$fields = benecaster_get_fields( '5', $episode_id );
Pass the entire array to a template:
// In your template file:
$interview = benecaster_get_fields( 'Interview Info', get_the_ID() );
foreach ( $interview as $key => $value ) {
if ( $value ) {
printf(
'<dt>%s</dt><dd>%s</dd>',
esc_html( $key ),
esc_html( $value )
);
}
}
Related
benecaster_get_field— read a single field value by ID- [
benecaster_field_value](/docs/benecaster_field_value/) — filter applied to each value before it is returned - Custom Fields — overview of the Field Groups system for podcasters