Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP order posts by number of facebook likes
<?php
// get fb likes from fb graph api
// put this block in single.php, or somewhere you have $post and have setup postdata [inside a Loop]
$obj = json_decode( file_get_contents( 'http://graph.facebook.com/?id='.get_permalink() ) );
$likes = $obj->shares;
update_post_meta($post->ID, '_fb_likes', $likes, false);
// make a Loop [in a template page or whatever] to query and display posts ordered by fb like popularity
$args = array(
'meta_key' => '_fb_likes',
'orderby' => 'meta_value_num',
'order' => 'desc',
);
$posts = get_posts( $args );
foreach( $posts as $post) : setup_postdata($post);
// etc etc
endforeach;
// thanks http://peterbending.com/wordpress/how-to-order-posts-by-number-of-facebook-likes/
@SoFvip

This comment has been minimized.

Copy link

commented Sep 30, 2011

Hi i have some problems with this code, my query show posts but dont order them by shares.

probably i place the part with json at wrong place, can you be moe specific in what loop i should put the code. this function is very iportant to me
and i dont have skills in php i just only know that this language exist :-).

my query code looks like this

'_fb_likes', 'orderby' => 'meta_value_num', 'order' => 'desc', ); $posts = get_posts( $args ); foreach( $posts as $post) : setup_postdata($post); // etc etc endforeach; ?>

and json code is before

id="post-">

thank you for your time

@stresslimit

This comment has been minimized.

Copy link
Owner Author

commented Sep 30, 2011

The important part of this query is that the _fb_likes metafield gets populated with a correct facebook graph number BEFORE you can query and have this order work.

You could run a query once on the whole posts database, or set a scheduled task to do this; or, if you put the top portion of the code in single.php [as mentioned above], it will add the _fb_likes meta field with the correct fb graph count each time you go to a permalink. So you can put this in, and then populate your data manually by you visiting a few permalink pages and then checking that the correct data is populated.

@SoFvip

This comment has been minimized.

Copy link

commented Sep 30, 2011

@Osz

This comment has been minimized.

Copy link

commented Sep 1, 2013

Hi, first of all thank you for this code.

But unfortunately I cannot install it. Please help me to order my wordpress posts by facebook, twitter shares.
(http://peterbending.com/wordpress/how-to-order-posts-by-number-of-facebook-likes/ does not work)

(here is an example http://www.internoetics.com/2012/05/16/count-shares-to-facebook-twitter-linkedin-googleplus/ , but acctualy it does not work too).
Can you write instruction please.

@natesimpson

This comment has been minimized.

Copy link

commented Apr 6, 2014

https://wordpress.org/plugins/wp-facebook-like-posts-order/

So can anyone please help me edit this plugin ( i'm not familar with git hub or how this works but am willing to pay )

I would like to extend and update this plugin to allow you to order post by facebook likes but inside widget include an option to select a category - so say i'm displaying a group/archive of posts by a specific category like author , basketball , college, books etc... I would like to be able to use the widget om each of the category archives but be able to show most popular post in widget with number of like with just that category not all posts...This would also arrange posts by #likes inside category archive... Please help!!! thanks!

post->ID; $data = file_get_contents('http://graph.facebook.com/?id='.$permalink); $json = $data; $obj = json_decode($json); $like_no = $obj->{'shares'}; $meta_values = get_post_meta($idpost, 'facebook_likes', true); if($like_no == null){$like_no = 0;} update_post_meta($idpost, 'facebook_likes', $like_no, false); return $content; ``` } add_action('the_content', 'update_facebook_likes'); /* query_posts('meta_key=facebook_likes&orderby=meta_value'); <iframe src="http://www.facebook.com/plugins/like.php?href=&send=false&layout=box_count&width=70&show_faces=true&action=like&colorscheme=light&font&height=90" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:75px; height:90px;" allowTransparency="true"></iframe>

*/
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.