Top 3 posts in last week ordered by Facebook and Twitter share counts

This is a bit of a multi-part question, so you’re getting a multi-part answer.

  • Getting the Facebook count can be done by making a request to https://graph.facebook.com/URL. This will return a json object which contains, among other things, the number of “shares”.

Working example which has 4 shares as of this writing: https://graph.facebook.com/http://ottopress.com/2011/wp-quickie-adding-chrome-voice-search/

One thing I didn’t document there was that if you leave the callback parameter blank, you get back a json object without the JSONP callback stuff.

Working example which has 29 twitter count as of this writing: http://urls.api.twitter.com/1/urls/count.json?callback=&url=http://ottopress.com/2011/wp-quickie-adding-chrome-voice-search/

  • Now that you have those URLs, you need to write some functions to periodically get them for each of your posts (presumably in the last week) and to then store that data in a postmeta field. You can use a wp-cron job or transients for their expiration times. Either way.

Once you have the data, and have stored it in a postmeta for each post in question, then selecting based on it is a matter of using the meta_key in the post query, along with an orderby=meta_value_num parameter. This will allow you to select posts and order them by the value you’re storing in the postmeta.


Leave a Comment