Funciones de recuperación masiva para obtener todos los valores de campo o todas las definiciones de campo para una publicación a la vez.
get_fields( $post_id )
Recupera todos los valores de campo para una publicación como un array asociativo.
Parámetros:| Parámetro | Tipo | Por Defecto | Descripción | ||
|---|---|---|---|---|---|
$post_id | int\ | string\ | false | false | ID de publicación, 'options', 'user_N', o false para la publicación actual |
array — array asociativo de 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 )
Obtiene todas las definiciones de campo (con valores) para una publicación.
Parámetros:| Parámetro | Tipo | Por Defecto | Descripción | ||
|---|---|---|---|---|---|
$post_id | int\ | string\ | false | false | ID de publicación, 'options', o false para la publicación actual |
array — array asociativo de 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,
];
}—