Sorting Posts by Facebook Likes in WordPress
Today I was assigned an exciting task for a few of our sites on WordPress.
Make «Mest populære» based on number of FB likes.
I thought this would make a great blog post since it would utilize some really neat WordPress functionality.
To make this easier let’s break this up into 2 parts.
Part 1 – Functions.php
Here’s what you’ll need to add to your theme’s functions.php or a plugin of some sort.
[php]
// Sort Posts by Facebook Likes
function mn_fb_like_sort_hourly() {
global $wpdb;
// Loop Through All Posts
$args = array( ‘posts_per_page’ => ‘-1′ );
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
// Get Facebook Likes From FB Graph API
$data = file_get_contents(‘http://graph.facebook.com/?id=’. get_permalink());
$obj = json_decode($data);
$like_no = intval($obj->{‘shares’});
// Add Facebook Likes to Post Meta
update_post_meta(get_the_ID(), ‘_mn_fb_likes’, $like_no);
endwhile;
wp_reset_postdata();
}
add_action(‘mn_fb_like_sort’, ‘mn_fb_like_sort_hourly’);
function mn_fb_like_sort_cron() {
if ( !wp_next_scheduled( ‘mn_fb_like_sort’ ) ) {
wp_schedule_event(time(), ‘hourly’, ‘mn_fb_like_sort’);
}
}
add_action(‘wp’, ‘mn_fb_like_sort_cron’);
[/php]
Let’s loop through all of our posts.
[php]
// Loop Through All Posts
$args = array( ‘posts_per_page’ => ‘-1′ );
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
[/php]
Now let’s use Facebook’s Graph API to get the number of likes.
[php]
// Get Facebook Likes From FB Graph API
$data = file_get_contents(‘http://graph.facebook.com/?id=’. get_permalink());
$obj = json_decode($data);
$like_no = intval($obj->{‘shares’});
[/php]
Let’s add the number of likes to post meta. We’ll need this to sort later.
[php]
// Add Facebook Likes to Post Meta
update_post_meta(get_the_ID(), ‘_mn_fb_likes’, $like_no);
[/php]
Lastly, let’s use wp_schedule_event() to update these hourly. We don’t need to update these on every page load, causing a decrease in site performance.
[php]
function mn_fb_like_sort_cron() {
if ( !wp_next_scheduled( ‘mn_fb_like_sort’ ) ) {
wp_schedule_event(time(), ‘hourly’, ‘mn_fb_like_sort’);
}
}
add_action(‘wp’, ‘mn_fb_like_sort_cron’);
[/php]
That’s it for functions.php. Let’s move on to displaying these in our template/theme somewhere.
Part 2 – Theme/Template
Here’s an example loop. Let’s get the 4 most liked posts.
[php]
// Loop Through All Posts
$args = array(
‘posts_per_page’ => ’4′,
‘meta_key’ => ‘_mn_fb_likes’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘DESC’
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
// Loop!
endwhile;
wp_reset_postdata();
[/php]
Let me know what you think or if you’ve accomplished this another way. Comments and/or criticism are always welcome!
Also, if your looking to get more involved with WordPress in and around the Oslo area check out the Oslo WordPress Meetup Group.
Great post! I was wondering where to put the second part??
I’m not a great programmer but would be really happy if you could tell me where to include the second code!
Looking forward to your answer!