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!
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->countand not inresponse->comments). Also you may wish to usefields=commentsto 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:The flow as I see it may be so:
fb_comment_countonce post is viewedquery_postswithmeta_keyto change the defaults.Once your posts have
facebook_comments_countmeta you can usequery_postsin The Loop: