Bulk retrieval functions for getting all field values or all field definitions for a post at once.
get_fields( $post_id )
Retrieve all field values for a post as an associative array.
Parameters:| Parameter | Type | Default | Description | ||
|---|---|---|---|---|---|
$post_id | int | string | false | false | Post ID, 'options', 'user_N', or false for current post |
array — associative array of field_name => value.
php
// Get all fields for the current post
$fields = get_fields();
// Returns: ['subtitle' => 'Hello', 'price' => '29.99', 'is_featured' => true, ...]
// Get all fields for a specific post
$product_data = get_fields( 42 );
// Get all option values
$options = get_fields( 'options' );
// Returns: ['site_logo' => [...], 'site_phone' => '+1-555-0123', ...]
// Use in a REST API response or JSON output
wp_send_json_success( get_fields( $post_id ) );
// Build a structured data array for JSON-LD
$product = get_fields( $post_id );
$json_ld = [
'@context' => 'https://schema.org',
'@type' => 'Product',
'name' => get_the_title( $post_id ),
'offers' => [
'@type' => 'Offer',
'price' => $product['price'] ?? '0',
'priceCurrency' => 'USD',
],
];
echo '<script type="application/ld+json">' . wp_json_encode( $json_ld ) . '</script>';get_field_objects( $post_id )
Get all field definitions (with values) for a post.
Parameters:| Parameter | Type | Default | Description | ||
|---|---|---|---|---|---|
$post_id | int | string | false | false | Post ID, 'options', or false for current post |
array — associative array of field_name => field_object.
php
$objects = get_field_objects( 42 );
// Render all fields automatically based on type
foreach ( $objects as $name => $field ) {
if ( $field['type'] === 'image' && $field['value'] ) {
echo '<figure>';
echo '<img src="' . esc_url( $field['value']['url'] ) . '" alt="' . esc_attr( $field['value']['alt'] ) . '">';
echo '<figcaption>' . esc_html( $field['label'] ) . '</figcaption>';
echo '</figure>';
} elseif ( $field['type'] === 'true_false' ) {
echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ';
echo $field['value'] ? 'Yes' : 'No';
echo '</p>';
} elseif ( $field['value'] ) {
echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ';
echo esc_html( $field['value'] ) . '</p>';
}
}
// Export all field definitions for documentation
$all_objects = get_field_objects( 42 );
$schema = [];
foreach ( $all_objects as $name => $obj ) {
$schema[] = [
'name' => $obj['name'],
'type' => $obj['type'],
'label' => $obj['label'],
'required' => $obj['required'] ?? false,
];
}—