update_field()

Description

This function is used to update a field value for a specific post.

Parameters

<?php update_field($selector, $value, $post_id); ?>
  • $selector (string) the field name or key (required)
  • $value (mixed) the value to save (required)
  • $post_id (mixed) Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

Usage

Basic (field name)

This example demonstrates how to update the value of a field called ‘views’ on the current post being viewed.

<?php

// get current count value
$count = (int) get_field('views');


// increase
$count++;


// update
update_field('views', $count);
    
?>

Basic (field key)

This example demonstrates how to achieve the same as above using the field’s key instead of it’s name. The field’s key should be used when saving a new value to a post (when no value exists). This helps ACF create the correct ‘reference’ between the value and the field’s settings.

Each value saved in the database is given a ‘reference’ of the field’s key. This allows ACF to connect a value with it’s field. ACF does this so it can format values when loaded based of the field type and settings. For example, the image field contains a setting to return an array of image data instead of the attachment ID.

<?php

// get current count value
$count = (int) get_field('field_123456');


// increase
$count++;


// update
update_field('field_123456', $count);
    
?>

Update a value from different objects

This example shows a variety of $post_id values to update a value from a post, user, term and option.

$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above

$value = update_field( 'my_field', 'my_value', $post_id );

Advanced

This example will demonstrate how to create a new post, and save multiple field values to it.

<?php 

// vars
$my_post = array(
	'post_title'	=> 'My post',
	'post_type'		=> 'post',
	'post_status'	=> 'publish'
);


// insert the post into the database
$post_id = wp_insert_post( $my_post );


// save a basic text value
$field_key = "field_123456";
$value = "some new string";
update_field( $field_key, $value, $post_id );


// save a checkbox or select value
$field_key = "field_1234567";
$value = array("red", "blue", "yellow");
update_field( $field_key, $value, $post_id );


// save a repeater field value
$field_key = "field_12345678";
$value = array(
	array(
		"sub_field_1"	=> "Foo",
		"sub_field_2"	=> "Bar"
	)
);
update_field( $field_key, $value, $post_id );


// save a flexible content field value
$field_key = "field_123456789";
$value = array(
	array( "sub_field_1" => "Foo1", "sub_field_2" => "Bar1", "acf_fc_layout" => "layout_1_name" ),
	array( "sub_field_x" => "Foo2", "sub_field_y" => "Bar2", "acf_fc_layout" => "layout_2_name" )
);
update_field( $field_key, $value, $post_id );

?>

Related