Sort ads by Popularity, Using facebook "likes"
Hi, i wanted to have the main loop in my index page sorted by popularity, but i didnīt want to sort using the cp_total_count customfield (wich stores the amount of times the ad has been viewed) because older ads tend to have more views regardless of their quality.
So i came up with the idea of using the facebook api to get the "like count" and sort using those numbers. (you have tu embbed a like button into each ad in order for this to work).
But given that older ads would still tend to have more "likes" i found it better to sort using a mixture of likes and views.
Basicaly the mixture is the ratio of likes to views, this way a newer good quality ad can be more popular than others even if it has less "likes" (if it has a higher ratio).
So, here is how i did it:
First i added the facebook like button to the facebook like box to "single-ad_listing.php"
(donīt ask me where to put it, it depends on where you want it to show up)
Code:
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php the_permalink(); ?>&layout=box_count&show_faces=false&width=75&action=like&font&colorscheme=light&height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; float:right; margin-top:5px; width:75px; height:65px;" allowTransparency="true"></iframe>
Then i added some code to get the facebook total_count (wich i think is the sum of comments, shares and likes of a given url) calculate the "popularity ratio" and store it in a custom field with the key: fb_popularity.
It also stores the facebook total count in another custom field with the key: fb_like_count
to single-ad_listing.php add this:
i added this just before:
Code:
<?php if(function_exists('selfserv_sexy')) { selfserv_sexy(); } ?>
Code:
<?php
if(get_post_meta($post->ID, 'cp_total_count', true)%10 == 0 || get_post_meta($post->ID, 'cp_total_count', true) <= 10 ){
require_once('facebook.php');
$facebook = new Facebook(array(
'appId' => 'YYYYYYYYYYYYYYYYYYY',
'secret' => 'XXXXXXXXXXXXXXXXX',
'cookie' => true,
));
$fql = "SELECT total_count FROM link_stat WHERE url='".get_permalink($post->ID)."'";
$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));
$fb_fans = $response[0]['total_count'];
update_post_meta($post->ID, 'fb_like_count', $fb_fans);
$popularity = ($fb_fans*100)/get_post_meta($post->ID, 'cp_total_count', true);
update_post_meta($post->ID, 'fb_popularity', round($popularity));
}
?>
To avoid makine too much calls to the facebook api, this will run the first 10 times the ad is viewed.
After that it will run once every 10 views.
If you are doing this, and you already have ads, you should change the conditions on the first line so it runs every time, then load all of your ads so they get updated, and then change the conditions back again.
Also, replace XXXXXXXX and YYYYYYYY with your own facebook api keys. if you donīt have them, google can tell you how and where to get them.
Then i edited the index.php file and replaced this
Code:
query_posts(array('post_type' => 'ad_listing', 'caller_get_posts' => 1, 'paged' => $paged));
With this:
Code:
query_posts(array('post_type' => 'ad_listing', 'caller_get_posts' => 1, 'paged' => $paged, 'meta_key' => 'fb_popularity', 'orderby' => 'meta_value'));
And thats it, i know the code isnīt perfect,i consider myself a noob y php, but it works.
I now have the best quality ads in the front page
and my visitors get a better impression about the site.
I hope my english wasnīt too bad.
Greetings from Chile