Support » Plugins » [WordPress Idea] Get Facebook Likes for every post

  • <?php
    // This needs to go inside of the loop
    // This is used to get the current ID of the post
    global $post;
    // The current post ID, which we need to use get_permalink
    $thePostID = $post->ID;
    // The current url of the post
    $url = get_permalink();
    /* This looks complicated but it is very easy. It is telling Facebook to do the query that we need that shows the "total_count" of Facebook likes for this single post. It only selects "total_count", but it could select more just by adding anything else. It is also just an sql query. select total_count from link_stat where url='$url' */
    $data = json_decode(file_get_contents("'$url'&format=json"));
    // this is the returned value
    $fb_stat =  $data[0]->total_count;
    // here we update the post meta with the information
    update_post_meta( $thePostID, 'fb_count', $fb_stat );

    I am trying to do a Top Rated by FB likes on my WordPress install. I was originally going to spider every page and get the likes and update it once a day. And then I thought well, the site is pretty active and it can update every time someone visits it, how can I do that.

    Well Facebook has there fbl query which you can get via curl or get contents. Which you can then decode with json. I think the above is pretty self explanatory.

    This is just the beginning and shouldn’t really be used on a busy server. I have been using this for a couple of weeks on my busy server and it isn’t much of a problem so far.

    If you were going to use this on a heavy server, I would check to see if Facebook likes have changed, and then update it. That won’t add any queries to the loop.

    Since I have implemented this over the weekend I made it so the posts are organized by top rated, you can see live example at

  • The topic ‘[WordPress Idea] Get Facebook Likes for every post’ is closed to new replies.