Created
December 4, 2014 01:44
-
-
Save nguyenDalex/c5660fc4c048d6f5bd3f to your computer and use it in GitHub Desktop.
set_custom_fields_value() inside of /models/posts.php for ACF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function set_custom_fields_value() { | |
global $json_api; | |
if ($json_api->include_value('custom_fields') && $json_api->query->custom_fields) { | |
// Query string params for this query var | |
$params = trim($json_api->query->custom_fields); | |
// Get all custom fields if true|all|* is passed | |
if ($params === "*" || $params === "true" || $params === "all") { | |
$wp_custom_fields = get_post_custom($this->id); | |
$this->custom_fields = new stdClass(); | |
// Loop through our custom fields and place on property | |
foreach($wp_custom_fields as $key => $val) { | |
if (get_field($key)) { | |
$this->custom_fields->$key = get_field($key); | |
} else if ($val) { | |
// Some fields are stored as serialized arrays. | |
// This method does not support multidimensionals... | |
// but didn't see anything wrong with this approach | |
$current_custom_field = @unserialize($wp_custom_fields[$key][0]); | |
if (is_array($current_custom_field)) { | |
// Loop through the unserialized array | |
foreach($current_custom_field as $sub_key => $sub_val) { | |
// Lets append these for correct JSON output | |
$this->custom_fields->$key->$sub_key = $sub_val; | |
} | |
} else { | |
// Break this value of this custom field out of its array | |
// and place it on the stack like usual | |
$this->custom_fields->$key = $wp_custom_fields[$key][0]; | |
} | |
} | |
} | |
} else { | |
// Well this is the old way but with the unserialized array fix | |
$params = explode(',', $params); | |
$wp_custom_fields = get_post_custom($this->id); | |
$this->custom_fields = new stdClass(); | |
foreach ($params as $key) { | |
if (isset($wp_custom_fields[$key]) && $wp_custom_fields[$key][0] ) { | |
$current_custom_field = @unserialize($wp_custom_fields[$key][0]); | |
if (is_array($current_custom_field)) { | |
foreach($current_custom_field as $sub_key => $sub_val) { | |
$this->custom_fields->$key->$sub_key = $sub_val; | |
} | |
} else { | |
$this->custom_fields->$key = $wp_custom_fields[$key][0]; | |
} | |
} | |
} | |
} | |
} else { | |
unset($this->custom_fields); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment