Archive

Archive for November, 2010

How to order posts by number of Facebook Likes

November 10th, 2010 No comments

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

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');

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:

<div class="fb_like_div"></div>

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:

global $wp_query;
$Magic_no = $wp_query-&gt;post-&gt;ID;

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

$json = $data;

$obj = json_decode($json);
$like_no = $obj-&gt;{'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);

}

Code for nice permalinks:

global $wp_query;
$Magic_no = get_permalink();
$Magic_no_postid = $wp_query-&gt;post-&gt;ID;
//echo get_permalink();
$data = file_get_contents('http://graph.facebook.com/?id='. $Magic_no);

$json = $data;

$obj = json_decode($json);
$like_no = $obj-&gt;{'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);

}

Now to display the posts Create a new page template and add this betwee the get_header and get_posts:

<!--?php<br /--> $featuredPosts1 = new WP_Query();
$featuredPosts1-&gt;query('cat=1&amp;posts_per_page=20&amp;meta_key=_my_key&amp;orderby=meta_value_num&amp;order=desc&amp;paged='.$paged);
while ($featuredPosts1-&gt;have_posts()) : $featuredPosts1-&gt;the_post(); ?&gt;
<div id="list_wrapp_inner"><a href="&lt;?php the_permalink(); ?&gt;">
<div id="list_wrapp_1_storycontent"><!--?php the_content(); ?--></div>
</a><a href="&lt;?php the_permalink(); ?&gt;"></a>
<div class="fb_like_div"></div>
</div>
<!--?php endwhile; ?-->
Share!
Categories: web development, Wordpress Tags: