Fields are usually filled in by editors, but sometimes code needs to write them — an importer, a form handler, a scheduled job. nxt_save_field_value() is that entry point.
What It Does
It writes a value through the field’s own sanitiser, so what lands in the database matches what the editor would have produced. It also checks that the current user may edit the object.
Before You Start
- The field exists and its group’s location rules match the object you are writing to.
- A logged-in user with permission to edit that object — see the limitation below.
- You know the field name and the shape its type expects.
Write a Value
- Call
nxt_save_field_value()with the field name, the value, and the object.
- Pass the value in the shape the field type stores — an array for checkbox and relationship, a string for text, an attachment ID for image.
- Check the return value before assuming it worked.
Display the Value
Writing, clearing and resetting:
<?php
// write
nxt_save_field_value( 'price', 425000, $post_id );
nxt_save_field_value( 'amenities', array( 'parking', 'garden' ), $post_id );
// clear the stored value
nxt_erase_field_value( 'price', $post_id );
// put the field back to its configured default
nxt_reset_field( 'price', $post_id );
Limitations
- The function checks the current user’s capability to edit the object. Under WP-CLI there is no logged-in user, so the write is refused — pass
--user=1when running a script through WP-CLI. - The value must match the field type’s stored shape. A checkbox given a plain string will not read back as you expect.
- Writing does not create the field. The field must already exist in a group whose rules match.
Troubleshooting
It returns false and nothing is saved
Most often a capability check: there is no current user, or that user cannot edit the object. Under WP-CLI add --user=1.
The value saves but reads back wrong
The shape is wrong for the type. Save an array for multi-value fields, an attachment ID for media fields, and the stored value (not the label) for choice fields.










