Skip to content

Commit 9992979

Browse files
Add register_post_meta example
Fixes #51
1 parent 3a8286c commit 9992979

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ For each object type — posts, or users, terms, comments, etc. — `$wp
100100

101101
## Working with registered meta in the REST API
102102

103-
The [`register_meta`](https://developer.wordpress.org/reference/functions/register_meta/) function simplifies the process of defining a meta field for a particular object type. By setting `'show_in_rest' => true` when registering a new meta key, that key will be accessible through the REST API. Note however that at this time **there is no way to register a meta field for a specific post type**: meta fields registered for the "post" object will appear on **all** custom post types, as well as the default post record. For this reason it is less broadly useful than `register_rest_field`.
104-
103+
The [`register_meta`](https://developer.wordpress.org/reference/functions/register_meta/) function simplifies the process of defining a meta field for a particular object type. By setting `'show_in_rest' => true` when registering a new meta key, that key will be accessible through the REST API.
105104

106105
### Read and write a post meta field in post responses
107106

@@ -129,6 +128,19 @@ This example shows how to allow reading and writing of a post meta field. This w
129128

130129
Note that for meta fields registered on custom post types, the post type must have `custom-fields` support. Otherwise the meta fields will not appear in the REST API.
131130

131+
### Post Type Specific Meta
132+
WordPress 4.9.8 adds support for registering meta for a specific post type or taxonomy by using the `register_post_meta` and `register_term_meta` functions. They follow the same rules as `register_meta` but require passing the desired object type as the first paramter. The following code would register the `my_meta_key` example above, but only for the `page` custom post type.
133+
134+
```php
135+
$args1 = array(
136+
'type' => 'string',
137+
'description' => 'A meta key associated with a string meta value.',
138+
'single' => true,
139+
'show_in_rest' => true,
140+
);
141+
register_post_meta( 'page', 'my_meta_key', $args1 );
142+
```
143+
132144

133145
## Adding Links to the API Response
134146
WordPress generates a list of links associated with the queried resource to make it easier to navigate to related resources.

0 commit comments

Comments
 (0)