FIXED: Get all custom fields the right way!
-
Now you can pass in either { * | true | all } and retrieve all of your custom fields.
Any of these will work
?json=true&custom_fields=*
?json=true&custom_fields=true
?json=true&custom_fields=allThere is also a fix for custom fields that are stored as serialized arrays… for us theme developers who do this. ??
You can replace the function or method set_custom_fields_value(){} with the following…
You can download the raw version here: https://codepad.org/WZVmodoB
// Updated <code>set_custom_fields_value()</code> to retreive all fields properly 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 ($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); } }
I think the author should feel free to include this…
Viewing 15 replies - 1 through 15 (of 15 total)
Viewing 15 replies - 1 through 15 (of 15 total)
- The topic ‘FIXED: Get all custom fields the right way!’ is closed to new replies.