How to order posts by number of Facebook Likes

I was recently asked to produce a facebook “like” page where by the website owner could add entries and visitors could like those entries via facebook. The entries could then be displayed in order of most likes. see http://purplepanda.eu/like/

Afetr a brief discussion the cient told me they have used wordpress before and where comfortable using it, so hey lets build the site for wordpress.

First off we need somewhere to store our number of facebook likes against the post so add the following to functions.php

[cc lang=”php” tab_size=”3″ lines=”40″]
function add_custom_field_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, ‘_my_key’, ‘0’, true);
}
}
add_action(‘publish_page’, ‘add_custom_field_automatically’);
add_action(‘publish_post’, ‘add_custom_field_automatically’);
[/cc]

This will automatically add a custom field called _my_key to any new posts with a default value of “0” the reason for the _ before my_key is to hide the custom field from the post editor.

Next you’ll need to add the facebook like button to your code no use adding the button from facebook.com you have to write your own add this code where you want the button to appear inside the Post loop:
[cc lang=”html” tab_size=”3″ lines=”40″]

[/cc]

Next we need to check how many likes we have against each post and update the custom field we created at the beginning “_my_key”.
This code is for Satndard Permalinks e.g p?=123.
For nice permalinks use the code underneath!
You can add this to your header.php file:
[cc lang=”php” tab_size=”3″ lines=”40″]
global $wp_query;
$Magic_no = $wp_query->post->ID;

$data = file_get_contents(‘http://graph.facebook.com/?id=’.bloginfo(‘url’).’?p=’. $Magic_no);

$json = $data;

$obj = json_decode($json);
$like_no = $obj->{‘shares’};
$meta_values = get_post_meta($Magic_no, ‘_my_key’, true);
//$meta_values = 1;
//if ($like_no == ‘2’) {
//echo $meta_values;
if ($like_no == $meta_values) {

} else if (empty($meta_values)) {
add_post_meta($Magic_no, ‘_my_key’, $like_no, true);
update_post_meta($Magic_no, ‘_my_key’, $like_no, false);

} else {
update_post_meta($Magic_no, ‘_my_key’, $like_no, false);

}
[/cc]

Code for nice permalinks:

[cc lang=”php” tab_size=”3″ lines=”40″]
global $wp_query;
$Magic_no = get_permalink();
$Magic_no_postid = $wp_query->post->ID;
//echo get_permalink();
$data = file_get_contents(‘http://graph.facebook.com/?id=’. $Magic_no);

$json = $data;

$obj = json_decode($json);
$like_no = $obj->{‘shares’};
$meta_values = get_post_meta($Magic_no_postid, ‘_my_key’, true);
//$meta_values = 1;
//if ($like_no == ‘2’) {
// echo “Number of Likes”.$meta_values;
if ($like_no == $meta_values) {

} else if (empty($meta_values)) {
add_post_meta($Magic_no_postid , ‘_my_key’, $like_no, true);
update_post_meta($Magic_no_postid , ‘_my_key’, $like_no, false);

} else {
update_post_meta($Magic_no_postid, ‘_my_key’, $like_no, false);

}
[/cc]

Now to display the posts Create a new page template and add this betwee the get_header and get_posts:
[cc lang=”php” tab_size=”3″ lines=”40″]
$featuredPosts1 = new WP_Query();
$featuredPosts1->query(‘cat=1&posts_per_page=20&meta_key=_my_key&orderby=meta_value_num&order=desc&paged=’.$paged);
while ($featuredPosts1->have_posts()) : $featuredPosts1->the_post(); ?>


[/cc]

Leave a Reply

Your email address will not be published. Required fields are marked *