Programmatically create, update, or remove field values from PHP code.
update_field( $selector, $value, $post_id )
Parameters:
| Parameter | Type | Default | Description | ||
|---|---|---|---|---|---|
$selector | string | — | Field name or field key | ||
$value | mixed | — | New value (type depends on field type) | ||
$post_id | int | string | false | false | Post ID, 'options', 'user_N', or false for current post |
int — the value record ID.
Value format by field type:
| Field Type | Expected Value |
|---|---|
text, textarea, email, url, password | string |
number, range | string or int/float |
image, file | Attachment ID (int) |
select, radio, button_group | Value string or array if multiple |
checkbox | Array of value strings |
true_false | true/false or 1/0 |
relationship, post_object | Post ID or array of post IDs |
taxonomy | Term ID or array of term IDs |
user | User ID or array of user IDs |
date_picker | 'Ymd' format string (e.g., '20241225') |
color_picker | Hex string (e.g., '#FF5733') |
gallery | Array of attachment IDs |
repeater | Array of row arrays |
php
// Update a text field
update_field( 'subtitle', 'New Subtitle', 42 );
// Update an image field (pass attachment ID)
update_field( 'hero_image', 155, 42 );
// Update a select field
update_field( 'product_status', 'in_stock', 42 );
// Update a checkbox field (array of values)
update_field( 'amenities', [ 'wifi', 'parking', 'pool' ], 42 );
// Update an options page value
update_field( 'site_phone', '+1-555-0123', 'options' );
// Update a user field
update_field( 'biography', 'Senior developer with 10 years experience.', 'user_5' );
// Bulk update in a loop
$products = get_posts( [ 'post_type' => 'product', 'numberposts' => -1 ] );
foreach ( $products as $product ) {
$current_price = get_field( 'price', $product->ID );
update_field( 'price', $current_price * 1.1, $product->ID ); // 10% increase
}
// Import data from CSV
$csv = array_map( 'str_getcsv', file( '/path/to/products.csv' ) );
$headers = array_shift( $csv );
foreach ( $csv as $row ) {
$data = array_combine( $headers, $row );
$post_id = wp_insert_post( [
'post_title' => $data['name'],
'post_type' => 'product',
'post_status' => 'publish',
] );
update_field( 'price', $data['price'], $post_id );
update_field( 'sku', $data['sku'], $post_id );
}delete_field( $selector, $post_id )
Delete a field value from the database.
Parameters:| Parameter | Type | Default | Description | ||
|---|---|---|---|---|---|
$selector | string | — | Field name | ||
$post_id | int | string | false | false | Post ID, 'options', or 'user_N' |
bool — true on success.
php
// Delete a single field value
delete_field( 'old_field', 42 );
// Delete an option value
delete_field( 'deprecated_setting', 'options' );
// Delete a user field
delete_field( 'temp_code', 'user_5' );
// Clean up fields when a post transitions to trash
add_action( 'wp_trash_post', function( $post_id ) {
delete_field( 'cached_summary', $post_id );
delete_field( 'cached_thumbnail', $post_id );
});
// Remove all Field Forge values for a post
FIELDFORGE_Field_Values::instance()->delete_all( $post_id );—