3

So what i have in mind is showing a list with most popular posts based on how many facebook comments they have. I already managed to make a function that counts based facebook graph how many comments a post has, but i am having problem with the query:

function fb_comment_count() {
global $post;
$url = get_permalink($post->ID);

$filecontent = file_get_contents('http://graph.facebook.com/?ids=' . $url);
$json = json_decode($filecontent);
$count = $json->$url->comments;
if ($count == 0 || !isset($count)) {
    $count = 0;
} ?>

<?php if ($count == 0) { ?>
         <span>No comment</span>
<?php } elseif ($count == 1) { ?>
         <span>One Comment</span>
<?php } elseif ($count > 1 ) { ?>
         <span><?php echo $count; ?> Comments</span>

Thanks!

ciprian
  • 443
  • 2
  • 8
  • 23
  • What is your question? Are there any errors? – DMCS Jan 19 '12 at 01:08
  • no errors. i just don't know how to make the query to show, lets say 5 posts with most facebook comments – ciprian Jan 19 '12 at 01:11
  • @ciprian, do you store the comments count somewhere into database or wordpress post meta? If not it'll gonna be VERY slow to fetch comments count for all your posts to only display most commented... – Juicy Scripter Mar 25 '12 at 13:55

2 Answers2

2

You may want to store number of comments to post meta-data so you'll be able to use it for sorting later.

BTW, your function will not work due to difference in response format you use and the real response. (number of comments is present in response->comments->count and not in response->comments). Also you may wish to use fields=comments to limit the response to only include details about comments without all the rest of data or using FQL query to retrieve only count of comments:

SELECT commentsbox_count FROM link_stat WHERE url = 'POST_URL'

The flow as I see it may be so:

  • Store number of comments within post-meta
  • Update number of comments calling fb_comment_count once post is viewed
  • Use query_posts with meta_key to change the defaults.
function fb_comment_count() {
  global $post;
  $url = get_permalink($post->ID);

  $query = "SELECT commentsbox_count FROM link_stat WHERE url = '{$url}'";
  $responseText = file_get_contents('https://graph.facebook.com/fql?q='.$query);
  $responseJson = json_decode($responseText);

  $commenteCount = $responseJson->data->commentsbox_count;
  update_post_meta($post->ID, 'facebook_comments_count, $commenteCount);
  // ...
}

Once your posts have facebook_comments_count meta you can use query_posts in The Loop:

query_posts('posts_per_page=5&meta_key=facebook_comments_count&orderby=meta_value&order=DESC')
Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
  • i changed `commentsbox_count` to `comment_count` then it worked for me – John Smith May 03 '13 at 10:27
  • where would this code go? in functions.php? also could you change update_post_meta( 7, 'fruit', 'banana' ) to add_post_meta( 7, 'fruit', 'banana', true ) || update_post_meta( 7, 'fruit', 'banana' ); – eliwedel Apr 13 '15 at 16:25
1

You will want to get to HTTP GET to http://graph.facebook.com/comments?ids= and it will returns an object with a data property. That data property will be a array of comment objects (see https://developers.facebook.com/docs/reference/api/Comment/)

For example:

http://graph.facebook.com/comments?ids=http://www.stackoverflow.com/

{
   "http://www.stackoverflow.com/": {
      "data": [
         {
            "id": "450042939888_21515527",
            "from": {
               "name": "Anidhya Ahuja",
               "id": "1172382999"
            },
            "message": "abc",
            "created_time": "2011-10-11T13:55:15+0000"
         },
         {
            "id": "450042939888_21515536",
            "from": {
               "name": "Anidhya Ahuja",
               "id": "1172382999"
            },
            "message": "wass",
            "created_time": "2011-10-11T13:55:48+0000"
         }
      ],
      "paging": {
         "next": "http://graph.facebook.com/comments?ids=http\u00253A\u00252F\u00252Fwww.stackoverflow.com\u00252F&limit=25&offset=25&__after_id=450042939888_21515536"
      }
   }
}
DMCS
  • 31,720
  • 14
  • 71
  • 104