Search code examples
wordpressadvanced-custom-fieldswordpress-rest-api

Wordpress ACF retrieve file data from REST API


I'm fairly new to WordPress and using both the ACF plugin and the REST API for the first time. I've created a custom post type with custom fields, one of which is a file field. I have the REST API enabled for the post type and am able to retrieve data, but the file field only includes the file ID. Is there any way to get the file URL without having to make an additional call for each post of that type?

The return value for that custom field is currently set to file array, although I also tried file URL, but that doesn't appear to impact the REST API.

An example of the formatted JSON returned for a test post:

[
   {
      "id":282,
      "date":"2022-08-10T16:52:58",
      "date_gmt":"2022-08-10T16:52:58",
      "guid":{
         "rendered":"https:\/\/[domain]\/?post_type=resourcelibrary_file&p=282"
      },
      "modified":"2022-08-10T16:52:58",
      "modified_gmt":"2022-08-10T16:52:58",
      "slug":"auto-draft",
      "status":"publish",
      "type":"resourcelibrary_file",
      "link":"https:\/\/[domain]\/resourcelibrary_file\/auto-draft\/",
      "template":"",
      "meta":[
         
      ],
      "vl_bok_primary_cat":[
         
      ],
      "vl_cat":[
         
      ],
      "acf":{
         "title":"PR Test",
         "author":"Test Person",
         "year":2022,
         "keywords":"test keyword list",
         "category":[
            21
         ],
         "bok_cats":[
            22,
            40
         ],
         "file":283
      },
      "_links":{
         "self":[
            {
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/resource_library_files\/282"
            }
         ],
         "collection":[
            {
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/resource_library_files"
            }
         ],
         "about":[
            {
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/types\/resourcelibrary_file"
            }
         ],
         "acf:term":[
            {
               "embeddable":true,
               "taxonomy":"vl_bok_primary_cat",
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/vl_bok_primary_cat\/40"
            },
            {
               "embeddable":true,
               "taxonomy":"vl_bok_primary_cat",
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/vl_bok_primary_cat\/22"
            },
            {
               "embeddable":true,
               "taxonomy":"vl_cat",
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/vl_cat\/21"
            }
         ],
         "wp:attachment":[
            {
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/media?parent=282"
            }
         ],
         "wp:term":[
            {
               "taxonomy":"vl_bok_primary_cat",
               "embeddable":true,
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/vl_bok_primary_cat?post=282"
            },
            {
               "taxonomy":"vl_cat",
               "embeddable":true,
               "href":"https:\/\/[domain]\/wp-json\/wp\/v2\/vl_cat?post=282"
            }
         ],
         "curies":[
            {
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      }
   }
]

Thank you!


Solution

  • I was able to find an answer in this post: https://support.advancedcustomfields.com/forums/topic/acf-rest-api-image-only-shows-attachment-id/

    Adding ?acf_format=standard to the end of my request did the trick, and that added an additional array of info beyond the ID for each file that included the URL.