Programmatically Update ACF Fields

Use update_field() and update_sub_field() to set ACF field values from code, with the correct value formats for each field type.

php hooks
|

Use update_field() and update_sub_field() to set ACF field values from code, with the correct value formats for each field type.

Basic field types — pass the value directly:

<?php
$post_id = 123;

// Text, textarea, number, email, URL.
update_field( 'company_name', 'Acme Corp', $post_id );

// True/false.
update_field( 'is_featured', true, $post_id );

// Date picker (Ymd format).
update_field( 'event_date', '20260415', $post_id );

// Select (single value).
update_field( 'status', 'active', $post_id );

// Select (multiple values) or checkbox.
update_field( 'categories', [ 'design', 'development' ], $post_id );

Image, file, and post object fields — pass the ID:

<?php
// Image or file field — pass attachment ID.
update_field( 'hero_image', 456, $post_id );

// Post object (single) — pass post ID.
update_field( 'related_page', 789, $post_id );

// Relationship — pass array of post IDs.
update_field( 'related_articles', [ 10, 20, 30 ], $post_id );

// Taxonomy — pass array of term IDs.
update_field( 'product_categories', [ 5, 12 ], $post_id );

Add a row to a repeater field:

<?php
add_row( 'team_members', [
    'name'  => 'Jane Smith',
    'role'  => 'Developer',
    'email' => 'jane@example.com',
], $post_id );

Update a specific repeater row (1-indexed):

<?php
// Update row 2's 'role' sub-field.
update_sub_field( [ 'team_members', 2, 'role' ], 'Senior Developer', $post_id );

Update an options page field:

<?php
update_field( 'company_phone', '03 9000 0000', 'option' );

Update a taxonomy term field:

<?php
update_field( 'brand_color', '#FF6600', 'term_42' );

Related Snippets

Stay Updated

Get ACF tips and new extensions in your inbox

No spam. Unsubscribe anytime.