5. update_field() and delete_field() | Field Forge - Custom Fields, Built for Speed
Download Log in

5. update_field() and delete_field()

Developer Guide

Programmatically create, update, or remove field values from PHP code.

update_field( $selector, $value, $post_id )

Parameters:
ParameterTypeDefaultDescription
$selectorstringField name or field key
$valuemixedNew value (type depends on field type)
$post_idintstringfalsefalsePost ID, 'options', 'user_N', or false for current post
Returns: int — the value record ID. Value format by field type:
Field TypeExpected Value
text, textarea, email, url, passwordstring
number, rangestring or int/float
image, fileAttachment ID (int)
select, radio, button_groupValue string or array if multiple
checkboxArray of value strings
true_falsetrue/false or 1/0
relationship, post_objectPost ID or array of post IDs
taxonomyTerm ID or array of term IDs
userUser ID or array of user IDs
date_picker'Ymd' format string (e.g., '20241225')
color_pickerHex string (e.g., '#FF5733')
galleryArray of attachment IDs
repeaterArray 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:
ParameterTypeDefaultDescription
$selectorstringField name
$post_idintstringfalsefalsePost ID, 'options', or 'user_N'
Returns: booltrue 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 );

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs