Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A function for sending Wordpress Advanced Custom Fields (ACF) Repeater Field, Flexible Content Field, Gallery Field data with Thermal API.
function north_cast_api_data($content) {
if (is_numeric($content)) $content = intval($content);
else {
$unserialized_content = @unserialize($content);
// we got serialized content
if ($unserialized_content !== false) {
// make sure that integers are represented as such, instead of str
foreach ($unserialized_content as $fn => &$c) {
if (is_numeric($c)) $c = intval($c);
$content = $unserialized_content;
return $content;
add_filter( 'thermal_post_entity', function($data, &$post, $state ) {
if( $state === 'read' ){
// get all of the post's metadata
$custom_fields_raw = (object) get_post_meta($post->ID);
// uncomment the following to include the raw data for testing
//$data->meta->custom_fields_raw = $custom_fields_raw;
// create an object for storing all the post data
$sections = new StdClass;
$custom_fields_additional = array();
foreach ($custom_fields_raw as $fieldName => $content) {
// remove any fields that start with an underscore, as it's a private one
if (substr((string)$fieldName, 0, 1) == '_') continue;
// if the string matches the format string_X_string, where X is any
// integer then we need to put it into an object
if (preg_match('_\d+_', (string)$fieldName, $indexes) > 0) {
// we need a type of field, an index of that field, and an item
$index = (int) $indexes[0];
$keys = explode('_'.$index.'_', (string)$fieldName);
$type = $keys[0];
$item = $keys[1];
// we need the type of this item, which is only stored in that type's
// serialized content
$type_ref = &$custom_fields_raw->$type;
$type_info = unserialize($type_ref[0]);
// now we need to create an array for that type
if (!$sections->$type) $sections->$type = array();
$object = &$sections->$type;
// add the object's type, if we have one.
if ($type_info[$index]) $object[$index]->type = $type_info[$index];
// check if we've got an integer or serialized data, and massage to the
// right type accordingly
$content = north_cast_api_data($content[0]);
// now add the content to the data array
$object[$index]->data->$item = $content;
// if we didn't match that format, this may be an additional custom field
// that needs to be included.
else {
$custom_fields_additional[$fieldName] = north_cast_api_data($content[0]);
$data->meta->sections = $sections;
$data->meta->custom_fields = (object) $custom_fields_additional;
return $data;
}, 10, 3);

This comment has been minimized.

Copy link

commented Mar 5, 2014

Hi! does this include the other fields in ACF? like the image fields etc..? Thanks!


This comment has been minimized.

Copy link
Owner Author

commented Mar 11, 2014

@Otakumouse: Yes, it should include all ACF fields, such as Images. It's been most heavily tested with content repeaters (Repeater Field, Flexible Content Field, etc) but should work with any field type.


This comment has been minimized.

Copy link

commented Apr 15, 2014

Big thanks for this gist! I was wondering how to do this exact thing and this works great.

However, I was receiving a Creating default object from empty value on line 61, so I made a fork with some object initialization before then to ensure there's an object in place to write to.


This comment has been minimized.

Copy link

commented Jul 3, 2014

Will this ever work with category specific custom fields? The meta field is empty for ACF fields set for taxonomies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.