Home › Forums › Feature Requests › Native ACF JSON API
I went searching for a way to access the content of a repeater field via JSON and turned up this thread on the old support forum: http://old.support.advancedcustomfields.com/discussion/4418/native-acf-json-api/p1
I’d like to resurrect it and add that, 1. I would find that functionality highly valuable and 2) It would definitely be worth paying for as a premium add-on.
Hi @sirjonathan
Looks like the simplest answer was hooking in and adding the custom fields like so:
add_filter('json_api_encode', 'json_api_encode_acf');
function json_api_encode_acf($response)
{
if (isset($response['posts'])) {
foreach ($response['posts'] as $post) {
json_api_add_acf($post); // Add specs to each post
}
}
else if (isset($response['post'])) {
json_api_add_acf($response['post']); // Add a specs property
}
return $response;
}
function json_api_add_acf(&$post)
{
$post->acf = get_fields($post->id);
}
http://wordpress.org/extend/plugins/json-api/other_notes/#Filter:-json_api_encode http://stackoverflow.com/questions/10132685/json-api-to-show-advanced-custom-fields-wordpress
The code is so small that I could not charge for it. Maybe you could package it up into a WP plugin?
Thanks
E
I just added a little variation to retrieve fields from the Option Page. I added the code in this StackOverflow answer http://stackoverflow.com/a/22983903/2011287
Where i use this code? And, how to return the latitude for example? Thanks
Hi Elliot,
code works fine, since now I get the structure out.
I’ve come across a weird issue with the output though.
Here is a snippet from the “custom field” part of the JSON output:
“custom_fields”: {
“gallerythumb”: [
“a:3:{i:0;s:2:\”90\”;i:1;s:2:\”92\”;i:2;s:2:\”91\”;}”
],
Tried to get the IDs out (“90”, “92”, “91”) so I can store it in an array to which I can reference the different “attachments”, since the urls don’t come with the “custom_fields”.
Can you spot what the issue is with the output? or does anyone else have a solution for this? Would love to hear some thoughts on this! 🙂
The topic ‘Native ACF JSON API’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.