0

I am using REST API plugin to create a JSON response to render posts on another website from this website. The issue I am facing is that in the JSON response, only the featured image media ID is returned, not the actual URL to the image. Is there a way to pass the media ID into a URL to use that in place of the actual URL? Here's an example of the JSON response:

[
   {
      "id":6,
      "date":"2017-11-30T18:19:11",
      "date_gmt":"2017-11-30T18:19:11",
      "guid":{
         "rendered":"http:\/\/mysite.com\/?p=6"
      },
      "modified":"2017-11-30T18:19:11",
      "modified_gmt":"2017-11-30T18:19:11",
      "slug":"post-2",
      "status":"publish",
      "type":"post",
      "link":"http:\/\/mysite.com\/2017\/11\/30\/post-2\/",
      "title":{
         "rendered":"Post 2"
      },
      "content":{
         "rendered":"",
         "protected":false
      },
      "excerpt":{
         "rendered":"",
         "protected":false
      },
      "author":1,
      "featured_media":0,
      "comment_status":"open",
      "ping_status":"open",
      "sticky":false,
      "template":"",
      "format":"standard",
      "meta":[

      ],
      "categories":[
         2
      ],
      "tags":[

      ],
      "_links":{
         "self":[
            {
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/posts\/6"
            }
         ],
         "collection":[
            {
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/posts"
            }
         ],
         "about":[
            {
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/types\/post"
            }
         ],
         "author":[
            {
               "embeddable":true,
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/users\/1"
            }
         ],
         "replies":[
            {
               "embeddable":true,
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/comments?post=6"
            }
         ],
         "version-history":[
            {
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/posts\/6\/revisions"
            }
         ],
         "wp:attachment":[
            {
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/media?parent=6"
            }
         ],
         "wp:term":[
            {
               "taxonomy":"category",
               "embeddable":true,
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/categories?post=6"
            },
            {
               "taxonomy":"post_tag",
               "embeddable":true,
               "href":"http:\/\/mysite.com\/wp-json\/wp\/v2\/tags?post=6"
            }
         ],
         "curies":[
            {
               "name":"wp",
               "href":"https:\/\/api.w.org\/{rel}",
               "templated":true
            }
         ]
      }
   }
]

From this response I would like to generate a URL to the featured image using the ID from featured_media

  • try the URL from wp:attachment – mmm Nov 30 '17 at 20:06
  • 1
    Have you seen this? – Milo Nov 30 '17 at 20:25
  • @Milo exactly what I was looking for, thanks! I had no idea this was built into Wordpress' core functionality now, explains why the plugin I was using hasn't been updated in 2 years XD. Thanks again! – APAD1 Nov 30 '17 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.