Instagram API Pagination – Get more than 20 and more than 33 photos
Sandbox Limitation
First of all, if your Instagram app is in a Sandbox mode, you are allowed to get only 20 latest photos of your own account.
There is two ways to get rid of this limitation – approve your Instagram app or use an implementation which doesn’t require access tokens.
How to Set the Number of Photos to Get from Instagram
Instagram API has count
parameter — if you don’t specify it in your API request, then by default it would be 20.
The example below shows how to change it to 30 for example:
https://api.instagram.com/v1/tags/wordcamp/media/recent?client_id=YOUR CLIENT ID HERE&count=30
This API call returns the latest 30 photos from Instagram tagged with wordcamp
.
But if you change 30 to 50, then you will be faced with another limitation. You will notice that Instagram API returns only 33 photos, not 50.
Before we continue to the next chapter, learn more how to get images by a hashtag here, find out how to do the same without an access token right here.
Instagram API Results Pagination. Show More than 33 photos.
Do you know what pagination is?
It is when a lot of posts (500 for example) can not be displayed on the one page for performance reasons and they are split into pages (by 10 posts for example).
If you are a WordPress developer you know what I am talking about (remember WP_Query).
Just imagine — you want to get photos by #wordcamp and this tag has 3K publications. Of course they can not be displayed at once.
PHP example
Function rudr_instagram_api_curl_connect()
connects to Instagram API, you can find it here.
Do not you know what Access Token is? Read here how to obtain it.
/*
* Get 33x2 = 66 photos
*/
$access_token = 'YOUR ACCESS TOKEN HERE';
$count = 33; // the number за photos per request (from 1 to 33)
$tag = 'wordcamp';
$result = rudr_instagram_api_curl_connect('https://api.instagram.com/v1/tags/' . $tag . '/media/recent?access_token=' . $access_token . '&count=' . $count);
foreach ($result->data as $post) {
echo '<a href="' . $post->link . '"><img src="' . $post->images->thumbnail->url . '"></a>';
}
$result_2 = rudr_instagram_api_curl_connect( $result->pagination->next_url ); // API request for the next 33 photos
// it is the same: $result = rudr_instagram_api_curl_connect( 'https://api.instagram.com/v1/tags/' . $tag . '/media/recent?access_token=' . $access_token . '&count=' . $count . '&max_tag_id=' . $result->pagination->next_max_tag_id );
foreach ($result_2->data as $post_2) {
echo '<a href="' . $post_2->link . '"><img src="' . $post_2->images->thumbnail->url . '"></a>';
}
So, it is simple — the more photos you need to get the more API requests you have to do ( 99 photos = 3 requests, 132 photos = 4 requests etc).
jQuery example
I prefer the jQuery way because there is no cURL connection required.
/*
* Get 60 photos
*/
var access_token = 'YOUR ACCESS TOKEN HERE',
tag='wordcamp',
count = 30; // number of photos per request (max 33)
$.ajax({
url: 'https://api.instagram.com/v1/tags/' + tag + '/media/recent',
dataType: 'jsonp',
type: 'GET',
data: {access_token: access_token, count: count},
success: function(result){
console.log(result);
for(x in result.data){
$('ul').append('
<li><img src="'+result.data[x].images.standard_resolution.url+'"></li>
');
}
$.ajax({
url: result.pagination.next_url, // we don't need to specify parameters for this request - everything is in URL already
dataType: 'jsonp',
type: 'GET',
success: function(result2){
console.log(result2);
for(x in result2.data){
$('ul').append('<li><img src="'+result2.data[x].images.standard_resolution.url+'"></li>');
}
},
error: function(result2){
console.log(result2);
}
});
},
error: function(result){
console.log(result);
}
});
Related

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hi, firstly thank you thank you and thank yout for you site and blog in which i have found some excellent and usefull material for my purpose, i’m creating a personal site with some photos that i need to extract from my instragram feed, i have already used part of your code included in
even if i have found some problems using code for tag
, from console log i see only
there’s no other thing, and obv i insert my own tagged word passed on
value.
I have tried to use this code for pagination but nothing appear and from inspect i see
can you give me some suggestions please?
Thank you so much
Hi,
Have you approved your Instagram access token?
hi, thank you for reply,can i submit to Instagram my access token even I haven’t a policy page?
I do not know, I never approved it
so basically your code in Javascript might work without Instagram approvation, is it correct?
actually I can have only tag file that are in my stream feed and till last 20 of them.
did you have some suggestions to help me?
thank you.
Regards
Everything is clearly described here.
Hi,
I am using Python to get the comments on a media using the following query:
https://www.instagram.com/p/BdX1tB9jgbD/?__a=1
But, I can’t get more than 30 comments for each media, and there is no way to go beyond that since there is no pagination and the end_cursor is not working in the api.
is there any advice in how to get more comments.
P.S. I am not develping anything, I just need to collect the comments for my research
Hi,
I have my first 20 photos displaying well but I can’t get the next ones from .next_url.
Is it because I’m in Sandbox mode ? (I use users/self and no tag filtering)
Thanks in advance !
Hi Luc,
Yes, it is.
Hi Misha
Love your blog and all work you’re doing with your IG plugin for WP. I’ve just purchased your plugin and wondered if you have any examples of an AJAX “load more” similar to what you have here on this post?
I’m using your PHP example to gain more control for the initial build, but looking to use AJAX to load additional posts:
I’m looking to load a certain hash-tag and would like to load 20-30 images up front and then allow the user to just load the next set on the same page for as many times they want to explore the results.
Thanks!
Hi Johnny,
Just noticed your comment, I usually check comments two times a week or so, if you need a faster response please create a ticket next time :)
To answer your question – at this moment my plugin doesn’t have a necessary functionality to fetch images “by pages”, sorry. Actually it had it in the previous year, but Instagram ruins my code every time.
Ah ok, no worries. I’ll build something myself then. Thanks for the response 😎
Hi Misha
Here’s a quick JavaScript example of how your plugin could be extended with a load more functionality, which I’m very interested in. I’m not really looking to build my own plugin but I do want to get beyond the 20 photo limit from my sandbox mode so hope that you’d consider extending with this functionality which I think it’s quite useful.
Cheers!
/Johnny
Hi Johnny,
Thank you for the code, I’m sure it worked a couple years ago, but now it won’t help.
Hi Misha
This is fully compliant with the Instagram API today: https://www.instagram.com/developer/endpoints/
Fully understand if this is not a priority so might just build this out myself 😎
Cheers!
/Johnny
Hi,
But API doesn’t allow to use most of the methods without an app approval…
Ahh. Maybe I misunderstood, I thought your plugin was an approved App and therefore you could integrate these.
No worries, thanks!
/Johnny
No, it works another way 🙂