Search code examples
facebookfacebook-graph-api

How to get the total number of posts in a Facebook page with Graph API?


I am using Facebook Graph API to crawl information from public page. The current problem is how to get the total number of posts on a Facebook page. If we go to /{page-id}/posts, it will only return 25 posts posted on this page, and no summary information mentioned the page's fields. I have check the previous answer, it seems that the only way is to count the number of items of each link in next and get total number. But it is very inefficient. Is there any method that can directly get the total post on a page other than FQL?


Solution

  • Facebook doesn't actively count the number of posts a page has made; depending on the page it could be an astronomical number.

    You would have to get all the posts and count through them yourself. I would use something like this.

    {PAGE}/posts?fields=id&limit=250
    

    It will return the smallest possible data set you need. You can't go above 250, you could with FQL but not with v2.1. Also you don't want FEED because that is an aggregation of the Page's Posts and the Posts made to the page by other users. This will return an object like this.

    {
     "data" : [
      PostData
      ... ],
     "paging" : {
      ...
      "next" : CursorURL
     }
    }
    

    This is the cursor URL so you can step down the groups of 250 posts in reverse chronological order.

    You can shortcut the counting by incrementing 250 every time you get a new cursor that also returns more posts. You will only have to count and add the results in the set with the 2nd to last cursor since the last cursor will return an empty data array.