user3703803 user3703803 - 3 months ago 18
reST (reStructuredText) Question

wordpress Rest api Image link in Post properties

As it is known, wordpress rest api works in a way that the Get request of any post gives us a post including a featured_media property, for Imaging purposes.
I have a situation where I need to get 100 Posts and I do that quite easy with 'wp-json/wp/v2/posts?per_page=100' and it works well, but the problem is that I need to get images for each post too. To do so I have to make each additional request for each post to get its Image.... so to get 100 posts, I have to send 101 requests and it is horrible waste of resources and time.
What should I do to include image link in the Post properties to save my 100 requests and do everything with just a single request.
Thanks

Answer

You need to modify the REST API response content - it can contain the information you want.

This was one of my examples, where I wanted to include a custom field value:

add_action( 'rest_api_init', 'slug_register_embed_youtube' );
// "venue" is a custom post type I created using the WP Types plugin
function slug_register_embed_youtube() {
    // first register the field with WP REST API
    register_rest_field( 'venue',
        'meta_data',
        array(
            'get_callback'    => 'venue_get_meta',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

function venue_get_meta($post, $field_name, $request) {
    // I wanted the value to appear in the response as "youtube_embed", 
    // and I wanted the "wpcf-youtube-embed" custom field's value there
    $meta_data = array(
        'youtube_embed' => get_post_meta($post[ "id" ], 'wpcf-youtube-embed')[ 0 ],
    );
    return $meta_data;
}

The code above modified the response:

{
    "id": 20,
    "date": "2016-06-08T16:37:23",
    "date_gmt": "2016-06-08T16:37:23",
    "guid": {
      "rendered": "http://www.example.com/wp/?post_type=venue&p=20"
    },
    "modified": "2016-06-20T11:45:22",
    "modified_gmt": "2016-06-20T11:45:22",
    "slug": "aquarium-club",
    "type": "venue",
    "link": "http://www.example.com/wp/venue/aquarium-club/",
    "title": {
      "rendered": "Aquarium Club"
    },
    "content": {
      "rendered": "<p>Aquarium Club. That&#8217;s OK. 7765e546uzfkjglh</p>\n<p>&nbsp;</p>\n<style>\n.gmap-iframe-container {\nmax-width: 100%;\n}\n.gmap-iframe {\nwidth: 100%;\nheight: 200px;\nmin-width:100%;\n}\n</style>\n<div class=\"gmap-iframe-container\">\n<iframe src=\"https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2695.565888030695!2d19.052103115616678!3d47.49836967917771!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4741dc402a04eee3%3A0x6869564cd433693c!2sAkv%C3%A1rium+Klub!5e0!3m2!1shu!2shu!4v1466147278469\" frameborder=\"0\" style=\"border:0\" allowfullscreen class=\"gmap-iframe\"></iframe></p>\n"
    },
    "excerpt": {
      "rendered": "<p>Aquarium Club. That&#8217;s OK. 7765e546uzfkjglh &nbsp;</p>\n"
    },
    "featured_media": 0,
    "menu_order": 0,
    "format": "standard",
    "tags": [],
    "meta_data": {
      "youtube_embed": "https://www.youtube.com/embed/xBW7DglTDGs"
    },
    "_links": {
      "self": [
        {
          "href": "http://www.example.com/wp/wp-json/wp/v2/venue/20"
        }
      ],
      "collection": [
        {
          "href": "http://www.example.com/wp/wp-json/wp/v2/venue"
        }
      ],
      "about": [
        {
          "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue"
        }
      ],
      "wp:attachment": [
        {
          "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=20"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "post_tag",
          "embeddable": true,
          "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=20"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  },

Notice, that I have this

"meta_data": {
    "youtube_embed": "https://www.youtube.com/embed/xBW7DglTDGs"
},

after the "tags" array.

You can find all the information for modifying response in WP REST API v2 here: http://v2.wp-api.org/extending/modifying/