Previously I was using FQL for this, but this is deprecated as of v2.1 and I'm moving over to v2.3 using the graph edge "likes".
Here is my URL:
https://graph.facebook.com/v2.3/<page_id>/likes?access_token=<access_token>&summary=true
This returns detail JSON with paging info - but it omits the total_count which is supposed to be returned when "summary=true" is used as described in the Facebook docs - you'll see what I mean.
Thanks @NativePaul
I spent almost two days to find a solution to get the Facebook Fan Page likes counter in numeric value to a shortcode. So I have amended a code I got from this link: http://www.internoetics.com/2015/07/13/display-number-facebook-page-likes-wordpress-php/
And amended it to work with the fan_count fields and here is the code for your reference:
/*
Display the Number of Facebook Page Likes in Plain Text with WordPress Shortcode (and PHP)
Shortcode: [fbpagelikes id="" appid="" appsecret="" cache="" n="1"]
*/
function internoetics_fb_pagelikes($atts) {
extract(shortcode_atts(array(
'id' => 'kenryscom',
'appid' => 'xxxxxxxxxxxxxxxx',
'appsecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'n' => 1,
'cache' => 3600 * 24 * 1
), $atts));
$fbcounthash = md5("$url.$cache.$appid.$appsecret.$n");
$fbcountrecord = 'fblikes_' . $fbcounthash;
$cachedposts = get_transient($fbcountrecord);
if ($cachedposts !== false) {
return $cachedposts;
} else {
$json_url ='https://graph.facebook.com/' . $id . '?fields=fan_count&access_token=' . $appid . '|' . $appsecret;
$json = file_get_contents($json_url);
$json_output = json_decode($json);
if($json_output->fan_count) {
$fan_count = $json_output->fan_count;
if ($n) $fan_count = number_format($fan_count);
set_transient($fbcountrecord, $fan_count, $cache);
return $fan_count;
} else {
return 'Unavailable';
}
}
}
add_shortcode('fbpagelikes','internoetics_fb_pagelikes');
You have to add the above code to the theme functions file and use the Shortcode anywhere as mentioned in the beginning of the code.
Anyone stumbling onto this answer now (April 2016) will get frustrated because the accepted answer no longer works in v2.6
?fields=likes and /likes now return the same result -> the pages that the page likes.
To get the number of fans, you now need to use fields=fan_count
https://graph.facebook.com/pepsius/?fields=fan_count&access_token=<access_token>
As you can see above, you can also make the request directly with the pagename, no need to fetch the pageID.
What you are looking for the total number of people that have liked the page or what the page has liked?
For example.
https://graph.facebook.com/v2.3/56381779049/likes?access_token=<access_token>&summary=true
Will return what the Page PepsiUS has liked.
https://graph.facebook.com/v2.3/56381779049?fields=likes&access_token=<access_token>
Will return the total number of people that have liked the page.
{"likes": 32804486,
"id": "56381779049"}
Varified here PepsiUS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With