20

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?

ekad
  • 13,718
  • 26
  • 42
  • 44
fuchs777
  • 936
  • 1
  • 13
  • 26
  • 1
    It should still be possible to do this with FQL, check out the answers to this question: http://stackoverflow.com/questions/14371949/facebook-api-comment-count-via-fql – Cormac Driver May 03 '13 at 13:11
  • 1
    Thank you very much! SELECT comment_info FROM stream WHERE post_id = ... works. If you post an answer I could accept it. – fuchs777 May 06 '13 at 08:09
  • @fuchs777 How can i get postId which you use like "125909647492772_502974003098530" ? – Harshal Kalavadiya Mar 10 '14 at 15:38
  • @HarshalKalavadiya there are a few different ways to get the postid, depending on your approach, just google for "facebook postid" – fuchs777 Mar 10 '14 at 17:12

9 Answers9

34

To get the count, add ?summary=1 at the end: https://graph.facebook.com/125909647492772_502974003098530/comments?summary=1

Veerle Struyf
  • 390
  • 3
  • 6
  • 1
    Thanks a lot for this solution. This should be the accepted answer. – Vishnu Sureshkumar Nov 23 '13 at 05:34
  • Indeed. So far this is the only way I've found to get likes/comments totals without without having to page through all of the results when you don't have an access token. Thank you! – jel Feb 28 '14 at 00:35
  • 2
    There's another answer that shows how you can do this via ?fields=likes.summary(true) from the list page, rather than getting the details for each post individually – Kevin Mar 27 '15 at 02:06
21

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 ,

Shirish Herwade
  • 10,601
  • 18
  • 66
  • 105
16

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)
Abhishek Goel
  • 15,517
  • 8
  • 81
  • 62
6

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.

Mahmoud Hanafy
  • 1,614
  • 1
  • 22
  • 30
5

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 = ...

Community
  • 1
  • 1
Cormac Driver
  • 2,381
  • 1
  • 10
  • 9
  • Yes, this is still working. It sounds like you may have run into a permissions issue. Make your you've requested the read_stream permission. – Cormac Driver Aug 28 '13 at 20:54
  • this gives incomplete data because of the behavior of the fb stream object. You have to use "limit" and "order by" to use it correctly. I would prefer the solution from Veerle Struyf. – nils petersohn Oct 07 '13 at 15:00
  • 7
    FB just announced a couple weeks ago that FQL has been deprecated and will not be available after the next couple API releases. – James Alexander May 13 '14 at 21:05
  • 1
    Please change the accepted answer to @Veerle Struyf answer. This method is now deprecated. – Nic Strong Jun 16 '15 at 02:21
  • Sorry, I lost track of this question, since I no longer work on the project using the facebook api. The answer seems correct so I changed the accepted answer. Thanks for the info @NicStrong – fuchs777 May 04 '16 at 07:45
3

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 = ...
Kiattisak Anoochitarom
  • 2,139
  • 1
  • 19
  • 15
3

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

ssuperczynski
  • 2,473
  • 2
  • 37
  • 57
-1

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

  • 2
    This wont work well since you get comments paged. You only get 25 comments per call after which you would need to call the next page. – fuchs777 Jul 12 '13 at 07:35
-1

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.

nhahtdh
  • 52,949
  • 15
  • 113
  • 149
Karm
  • 19
  • 4