• Hello,

    I’m using ACF with ACF Photo Gallery Field.
    I would like to output the data from ACF Photo Gallery Field to REST API but get an empty array in postman.
    My code is:

    function albums_endpoint( $request_data ) {

    // setup query argument
    $args = array(
    ‘post_type’ => ‘albums’,
    ‘posts_per_page’=>-1,
    ‘numberposts’=>-1
    );

    // get posts
    $posts = get_posts($args);

    // add custom field data to posts array
    foreach ($posts as $key => $post) {
    $posts[$key]->acf = get_fields($post->ID);
    $posts[$key]->link = get_permalink($post->ID);
    $posts[$key]->image = get_the_post_thumbnail_url($post->ID);
    $posts[$key]->gallery = acf_photo_gallery(‘ACF_FIELD_NAME’, $post->ID);
    }
    return $posts;
    }

    // register the endpoint
    add_action( ‘rest_api_init’, function () {
    register_rest_route( ‘albums/v1’, ‘/albums/’, array(
    ‘methods’ => ‘GET’,
    ‘callback’ => ‘albums_endpoint’,
    )
    );
    });

    Api output ==

    [
    {
    “ID”: 33,
    “post_author”: “1”,
    “post_date”: “2019-01-05 18:45:06”,
    “post_date_gmt”: “2019-01-05 18:45:06”,
    “post_content”: “”,
    “post_title”: “The first test album”,
    “post_excerpt”: “”,
    “post_status”: “publish”,
    “comment_status”: “closed”,
    “ping_status”: “closed”,
    “post_password”: “”,
    “post_name”: “brouillon-auto”,
    “to_ping”: “”,
    “pinged”: “”,
    “post_modified”: “2019-01-05 20:39:26”,
    “post_modified_gmt”: “2019-01-05 20:39:26”,
    “post_content_filtered”: “”,
    “post_parent”: 0,
    “guid”: “https://gyuto.local/?post_type=albums&p=33”,
    “menu_order”: 0,
    “post_type”: “albums”,
    “post_mime_type”: “”,
    “comment_count”: “0”,
    “filter”: “raw”,
    “acf”: {
    “album_title”: “The first test album”
    },
    “link”: “https://gyuto.local/albums/brouillon-auto/”,
    “image”: false,
    “gallery”: []
    }
    ]

    How can I solve this and get the gallery field to display?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Navneil Naicker

    (@navzme)

    Hi, are you replacing ACF_FIELD_NAME the with whatever ACF field name you have in the acf_photo_gallery(‘ACF_FIELD_NAME’, $post->ID);

    Thread Starter tanuki1986

    (@tanuki1986)

    Thank you for your reply,

    I tried as well but I always got an empty array as output

    function albums_endpoint( $request_data ) {

    // setup query argument
    $args = array(
    ‘post_type’ => ‘albums’,
    ‘posts_per_page’=>-1,
    ‘numberposts’=>-1
    );

    // get posts
    $posts = get_posts($args);

    // add custom field data to posts array
    foreach ($posts as $key => $post) {
    $posts[$key]->acf = get_fields($post->ID);
    $posts[$key]->link = get_permalink($post->ID);
    $posts[$key]->image = get_the_post_thumbnail_url($post->ID);
    $posts[$key]->gallery = acf_photo_gallery(‘GalleryPlugin’, $post->ID);

    }
    return $posts;
    }

    // register the endpoint
    add_action( ‘rest_api_init’, function () {
    register_rest_route( ‘albums/v1’, ‘/albums/’, array(
    ‘methods’ => ‘GET’,
    ‘callback’ => ‘albums_endpoint’,
    )
    );
    });

    {
    “ID”: 33,
    “post_author”: “1”,
    “post_date”: “2019-01-05 18:45:06”,
    “post_date_gmt”: “2019-01-05 18:45:06”,
    “post_content”: “”,
    “post_title”: “The first test album”,
    “post_excerpt”: “”,
    “post_status”: “publish”,
    “comment_status”: “closed”,
    “ping_status”: “closed”,
    “post_password”: “”,
    “post_name”: “the-first-test-album”,
    “to_ping”: “”,
    “pinged”: “”,
    “post_modified”: “2019-01-10 10:48:14”,
    “post_modified_gmt”: “2019-01-10 10:48:14”,
    “post_content_filtered”: “”,
    “post_parent”: 0,
    “guid”: “https://gyuto.local/?post_type=albums&p=33”,
    “menu_order”: 0,
    “post_type”: “albums”,
    “post_mime_type”: “”,
    “comment_count”: “0”,
    “filter”: “raw”,
    “acf”: false,
    “link”: “https://gyuto.local/albums/the-first-test-album/”,
    “image”: “https://gyuto.local/wp-content/uploads/2019/01/lightscape-775397-unsplash.jpg”,
    “gallery”: []
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘ACF Photo Gallery Field to REST API’ is closed to new replies.