Skip to content

Commit b198f18

Browse files
Document default metadata support
1 parent c7fa82c commit b198f18

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

extending-the-rest-api/modifying-responses.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,22 @@ This would result in two meta database rows. The first containing `[ "WordPress"
367367

368368
If the existing value for a meta field does not validate against the registered type and schema, the value for that meta field will be returned as null. If that null value is passed back to the API when doing an update request, you'll receive a `rest_invalid_stored_value` error: `The %s property has an invalid stored value, and cannot be updated to null.`. To fix this, either update the meta key with a valid value or omit that property from your request.
369369

370+
### Default Metadata Values
371+
372+
WordPress 5.5 adds [official support](https://make.wordpress.org/core/2020/08/04/registering-default-values-for-meta-data/) for specifying a default value for metadata if no value is defined. For example, with this code snippet the `price` meta field will be set to `0.00` in the REST API response if a value is not yet.
373+
374+
```php
375+
register_post_meta(
376+
'product',
377+
'price',
378+
array(
379+
'single' => true,
380+
'type' => 'string',
381+
'default' => '0.00',
382+
)
383+
);
384+
```
385+
370386
## Adding Links to the API Response
371387
WordPress generates a list of links associated with the queried resource to make it easier to navigate to related resources.
372388

0 commit comments

Comments
 (0)