You can use Types filters to expose your custom fields to WordPress REST API. By default this option is disabled.
The integration works only if Toolset Types plugin is active. Go to the Toolset -> Settings page and on Custom Content tab enable the REST API option. This will add a toolset-meta property with custom fields to posts, users and terms that show in REST responses.
Enabling exposing to REST API
If this option is enabled all custom fields will be exposed for all post types, taxonomies and user roles. All of the custom fields will show up in the REST API. You can use PHP filters to limit the data that is going to be exposed.
Filters for specific field groups and fields
First of all, you need to hook into the toolset_rest_run_exposure_filters filter and make sure it returns true:
add_filter( 'toolset_rest_run_exposure_filters', '__return_true' );
There are two PHP filters you can use to limit data exposure:
How the integration works
In all REST API routes that return posts, users and terms, another property toolset-meta is added. This property contains an object with custom field values. Each property of this object is named by custom field slugs and not meta_keys. This means that they do not contain the wpcf prefix. The code below will show a post with ID 1037 which has custom fields in two groups.
{ 'id': 1037, 'date': '2019-03-22T08:45:59', 'date_gmt': '2019-03 -22T08:45:59', 'guid': { 'rendered': 'http://localhost/?p=1037' }, 'modified': '2019-03-22T08:45:59', 'modified_gmt': '2019-03-22T08:45:59', 'slug': 'types-1767', 'status': 'publish', 'type': 'post', 'link': 'http://localhost/2019/03/types-1767/', 'title': { 'rendered': 'types-1767' }, 'content': {...}, // 2 items 'excerpt': {...}, // 2 items 'author': 1, 'featured_media': 0, 'comment_status': 'open', 'ping_status': 'open', 'sticky': false, 'template': 'format': 'standard', 'meta': [], 'categories': [ ... ], // 1 item 'tags': [], 'toolset-meta': { 'types-1995-conditioned': { 'phone-90937e4d': { 'type': 'phone', 'raw': '' } }, 'types-1995': { 'types-1995-field': { 'type': 'textfield', 'raw': '' } } } '_links' : { ... } // 10 items }
The data format of custom field values depends on the field type. The output will always contain the following two elements:
- type – slug of the field type
- raw – element with the raw value
Other array elements are added depending on the field type.
Formatted output for single and repeatable fields
For single-value fields, the extra keys will be added directly to the output. For repeatable fields, a key repeatable will be added. This field will have an array of single field values. Each of these values will contain the raw value again and then the formatted value with the selected key.
Let us take a look at two examples. In the first one, you have a single-value field with a raw field value 'a'
. Its field value in the REST response is:
'field-slug': { 'type': 'field_type_slug', 'raw': 'a', 'formatted_value': 'a_formatted' }
Note that the formatted_value
property is just an example and it depends on the field type.
In the second one, you have a repeatable field with a raw field value [ 'a', 'b', 'c' ]
. Its field value in the REST response is:
'field-slug': { 'type': 'field_type_slug', 'raw': [ 'a', 'b', 'c' ], 'repeatable': [ { 'raw': 'a', 'formatted_value': 'a_formatted' }, { 'raw': 'b', 'formatted_value': 'b_formatted' }, { 'raw': 'c', 'formatted_value': 'c_formatted' } ] ];
Field types and their value format in REST API
Field type defines what value format will be used in REST API. Keep in mind that not all fields can be repeatable.
Name | Raw data structure | Can be repeatable | Data in REST |
---|---|---|---|
audio | URL | Yes |
attachment_id ⇒ $id
|
checkbox | value of the checkbox per field settings | No | raw only |
checkboxes | cumbersome, undocumented serialized array | No |
checked ⇒ array of “value to store” of checked options
formatted ⇒ A comma-separated list of field values as per field definition.
|
colorpicker | raw (HEX color code) | Yes | raw only |
date | unix timestamp | Yes |
formatted ⇒ string using the date format
|
raw | Yes | raw only | |
embed | URL | Yes | raw only |
file | URL | Yes |
attachment_id ⇒ $id
|
google_address | raw (string with the address) | Yes | raw only |
image | URL | Yes |
attachment_id ⇒ $id
|
numeric | raw | Yes | raw only |
phone | raw | Yes | raw only |
post | not stored in metadata, but as an association to the current post | No |
raw only (ID of the related post)
|
radio | value of the selected option per field settings | No | raw only |
select | value of the selected option per field settings | No | raw only |
skype | raw / legacy serialized array | Yes |
skypename ⇒ skype name even if the field has a legacy data structure
|
textarea | raw | No | raw only |
textfield | raw | Yes | raw only |
url | URL | Yes | raw only |
video | URL | Yes |
attachment_id ⇒ $id
|
wysiwyg | raw | No | raw only |