How to add Facebook stats to WordPress custom post types

When you’re creating a WordPress site with custom post types, it’s often useful to know how many likes, shares and comments each post is getting on Facebook. There are one or two plugins that will give you this information, but they haven’t been updated in a while, and don’t always work too well. But it’s relatively easy to code your own. Here’s the end result, as seen at the Evangelical Magazine site.:

First, we need to write the function that will actually get the stats from Facebook. Facebook provides a very simple public API that requires no authentication or registration, and it returns the stats in JSON format. I’m storing each stat in a separate entry in post meta (as that makes it easy to sort on, which we’ll need later). But we don’t want the stats cached indefinitely, so each post also has a transient that records whether or not the stats are fresh. The transient will last for up to a week, although it the post is less than a week old, it will only last for a shorter time. With the code below, if the transient is present, we’ll read the stats from the post meta, if it’s not present, we’ll look the stats up from the Facebook API.

Now that’s done, we can set up all the filters and actions we need. There are quite a few of them, but most of them are pretty simple. I’ll explain each filter/action as we go.

First, we need to tell WordPress which columns to add to which custom post type. For that, we need the manage_edit-{custom_post_type}_columns filter. In this demo, my custom post type is called wcs_article, so the filter is manage_edit-wcs_article_columns. We simply add the elements we need to the $columns array.

Next, we need to output the content of each column. We do that on the manage_{custom_post_type}_posts_custom_column, which for us is manage_wcs_article_posts_custom_column. The function checks to see whether the post has been published. If it has, it gets the stats, and then outputs whichever variable is required.

And that’s actually all that we need to display the stats. You could stop there if you want. But I find it helpful to have the columns sortable, and to do that we need a few more filters and actions. The first filter is manage_edit-{custom_post_type}_sortable_columns, which for us is manage_edit-wcs_article_sortable_columns. The filter receives a list of the columns that can be sorted, and we simply add our columns to the list.

The second part of this puzzle is to modify the WordPress query so that the sort actually takes place. We can do that on the pre_get_posts action. The function will check that we’re in admin mode, and that we’re on the right screen for this custom post type. If we are, and the query contains an ‘orderby’ request for one of our column names, we amend the query by setting the orderby value to meta_value_num onthe appropriate column.

The code is now complete for adding, displaying and sorting by Facebook stats. The final piece of the jigsaw is the ability to re-check the stats before the cache expires. To do that we’ll add a new action in post_row_actions. The first function filters the existing actions, and adds a new one, which I’ve labelled ‘Recalc FB’. Theoretically, we could just add a query parameter to the URL of the current screen, but the code below also retains several existing parameters, to ensure that after the action we end back in the same place as when we began.

The link is now in place, we just need the behind the scenes work to actually refresh the cache. The code only needs to delete the transient. If there’s no transient, the existing code will be forced to download new data. We should probably beef this up by adding nonces, but as a proof of concept its more than adequate.

And that’s it. It’s rough-and-ready, but does the job pretty well. The one obvious improvement (beyond some security checks) would be to add the option to refresh the cache for all posts, rather than do it one at a time. I’ll leave that task to you :-).