Results 1 to 3 of 3

Thread: Sort ads by Popularity, Using facebook "likes"

  1. #1
    Thread Starter
    ricardo's Avatar
    Join Date
    May 2010
    Location
    Chile
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Lightbulb 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(); ?>&amp;layout=box_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;font&amp;colorscheme=light&amp;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

  2. #2
    jomarkosabel's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    41,008
    Thanks
    170
    Thanked 3,409 Times in 3,280 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!
    Please help our moderating team work more efficiently by not sending us support questions via PM. You can read more about how AppThemes support works here. However, you can send a PM to follow up and remind me if I missed your support request/thread.

    Thank you and have a nice day.

  3. #3
    Thread Starter
    ricardo's Avatar
    Join Date
    May 2010
    Location
    Chile
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    You must be an AppThemes customer and logged in to view this response. Join today!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 9
    Last Post: February 25th, 2013, 10:29 AM
  2. Please help me to get Facebook Likes
    By helmuc in forum Report ClassiPress Bugs
    Replies: 2
    Last Post: July 11th, 2011, 04:16 AM
  3. [SOLVED] "404 Not Found" page when attempting to "post an ad" or "join now"
    By tradingpoint in forum Report ClassiPress Bugs
    Replies: 3
    Last Post: March 21st, 2011, 08:51 PM
  4. Replies: 6
    Last Post: October 2nd, 2010, 11:50 AM