20
votes

seems Facebook changed the result of posts, few weeks ago it was possible to read the comment count from the post directly

https://graph.facebook.com/125909647492772_502974003098530

you got something like

...
"comments": {
"data": [
  {
    "id": "502974003098530_78616446", 
    "from": {
      "name": "Mathias Fritz", 
      "id": "526559276"
    }, 
    "message": "saugeil!", 
    "can_remove": false, 
    "created_time": "2013-03-26T14:58:01+0000", 
    "like_count": 1, 
    "user_likes": false
  }
], 
"paging": {
  "cursors": {
    "after": "MQ==", 
    "before": "MQ=="
  }
}, 
"count": 1

but now the count is missing.

I did some research on the graph documentation but the only change in that direction seems to be that comments can have comments now... and those comments are counted in a field named comment_count.

Is there still a way to get the total comment count?

9
It should still be possible to do this with FQL, check out the answers to this question: stackoverflow.com/questions/14371949/… - Cormac Driver
Thank you very much! SELECT comment_info FROM stream WHERE post_id = ... works. If you post an answer I could accept it. - fuchs777
@fuchs777 How can i get postId which you use like "125909647492772_502974003098530" ? - Harshal Kalavadiya
@HarshalKalavadiya there are a few different ways to get the postid, depending on your approach, just google for "facebook postid" - fuchs777

9 Answers

21
votes

I was having same problem, just adding likes.summary(true),comments.summary(true) in parameter in against "fields" worked for me.

e.g. I used https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN&fields=story,from,story_tags,likes.summary(true),comments.summary(true)

instead of https://graph.facebook.com/me/feed?access_token=ACCESS_TOKEN

Also you can add other parameters if you want; separated by a ,

16
votes

summary=true is what you are looking for

Get likes count :

114916098537132_1265715836790480/likes?summary=true

Get comments count

114916098537132_1265715836790480/comments?summary=true

Get shares count :

114916098537132_1265715836790480?fields=shares

And last [ combining all 3 ]

114916098537132_1265715836790480?fields=shares,likes.summary(true),comments.summary(true)

Improved version ( add limit(0) to removes list of likes and get only summary ):

114916098537132_1265715836790480?fields=shares,likes.limit(0).summary(true),comments.limit(0).summary(true)
6
votes

This works perfectly with me:

fields=shares,created_time,comments.summary(true).limit(0)

This return comments count at summary and return 0 comments at the same time which is perfect as you only need the comment count.

5
votes

You can get total comment count via FQL. See this question below as reference:

Facebook API - comment count via FQL

Here's the query you need: SELECT comment_info FROM stream WHERE post_id = ...

3
votes

If you'd like to count everything on Facebook. (That number is visible for Facebook's User)

You should use FQL (Facebook Query Language) instead of Graph API.

Facebook Query Language Reference

This situation you should to query

SELECT comment_info FROM stream WHERE post_id = ...
3
votes

You can do such requests:

{pageid}/posts?fields=comments.summary(1){id}

It will return a list of posts including comments count of each post. Here I have returned only the comment id because I only need the number of comments per post, but of course you can include many other fields:

{pageid}/posts?fields=comments.summary(1){id,message},id

-1
votes

Or to make less changes to you excisting code, use:

    $.each(json.data,function(i,fb){
    ...
       var commentsCount = 0
       if(fb.comments!=undefined){
          commentsCount=fb.comments.data.length
       }
    ...
    }

commentsCount holds number of comments for active child

-1
votes

Try the following:

{
    "data": [
        {
            "id": "447235535389660_1226199",
            "from": {
                "name": "Harjeet Walia",
                "id": "100004980601083"
            },
            "message": "Price",
            "can_remove": false,
            "created_time": "2013-09-06T10:39:01+0000",
            "like_count": 0,
            "user_likes": false
        },
        {
            "id": "447235535389660_1226152",
            "from": {
                "name": "Shoba Dhyani Jakhmola",
                "id": "100000906896060"
            },
            "message": "baap re kitna mehnga !",
            "can_remove": false,
            "created_time": "2013-09-06T10:05:09+0000",
            "like_count": 0,
            "user_likes": false
        }
    ],
    "paging": {
        "cursors": {
            "after": "MQ==",
            "before": "NA=="
        }
    }
}

then

int commentCount = <JsonNode Var with above data>.path("comments").path("data").size();

Here commentCount will give the number of comments.