Support

Account

Home Forums Feature Requests Native ACF JSON API

Helping

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.

    • Elliot

    • September 5, 2013 at 12:41 pm

    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

    • MagnusN

    • December 17, 2015 at 4:12 am

    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! 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.