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' );

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.

How the integration works
{
	"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 value in the REST response - Single-value fields
'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 value in the REST response - Repeatable fields
'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.

NameRaw data structureCan be repeatableData in REST
audioURLYes
attachment_id ⇒ $id
checkboxvalue of the checkbox per field settingsNoraw only
checkboxescumbersome, undocumented serialized arrayNo
checked ⇒ array of “value to store” of checked options
formatted ⇒ A comma-separated list of field values as per field definition.
colorpickerraw (HEX color code)Yesraw only
dateunix timestampYes
formatted ⇒ string using the date format
emailrawYesraw only
embedURLYesraw only
fileURLYes
attachment_id ⇒ $id
google_addressraw (string with the address)Yesraw only
imageURLYes
attachment_id ⇒ $id
numericrawYesraw only
phonerawYesraw only
postnot stored in metadata, but as an association to the current postNo
raw only (ID of the related post)
radiovalue of the selected option per field settingsNoraw only
selectvalue of the selected option per field settingsNoraw only
skyperaw / legacy serialized arrayYes
skypename ⇒ skype name even if the field has a legacy data structure
textarearawNoraw only
textfieldrawYesraw only
urlURLYesraw only
videoURLYes
attachment_id ⇒ $id
wysiwygrawNoraw only