Displaying Your Popular Posts Using Jetpack

jetpack-logo-1080x650Every site owner who is posting fresh, new content frequently wants to have their content read on a regular basis. It does not necessarily matter if it is a returning reader or a brand new one, (though returning readers are awesome!), what matters is that someone is consuming what you have to post about.

We also appreciate our site analytics and statistics. These help us break down what is going on, who is reading what, what sections of the website get the most traffic, and shape our decisions in the future. For that, we tend to go for either Automattic’s Jetpack plugin or Google Analytics.

For the purpose of our post today, we are going to focus on Jetpack and the statistics module that comes with it. With this, we will explore how to query for the site’s popular posts “for the past X days” and some ways you could display results to users. This post assumes that you have some experience with PHP and WordPress theme development, as well as the site you are developing for has had Jetpack stats enabled for at least a little while. That way you will have stats recorded and available to query for. If it is a new install or still in development, results may be slim at first.

Installing our plugin

First things first, we need to get our PHP class installed and activated on your WordPress install. I have made it into a convenient WordPress plugin for easy activation. You can download the plugin, and if you just want to see visually what is in the plugin, you can use this GitHub gist with the same contents.

Creating our object

Once you have installed and activated, we are ready to get to work using it. The first thing we need to do is instantiate a new object with some parameters for what we want the class to fetch.

$popular_post_object = new WDS_JetPack_Popular_Posts( array( 'count' => 5, 'days' => 2, 'transient_affix' => '_mypp' ) );

We have three parameters available for this, and we will want to pass them in as an array. The first parameter is ‘count’ which is for how many posts you want to be returned, and defaults to 5. The second is ‘days’ which is for how many days you want to compare between and it defaults to 2. Last is ‘transient_affix’ that is used as part of the WordPress transient used that will temporarily store the results in, this one defaults to just ‘_popular’ in most places, but adds the “current queried object ID” if one exists. This allows for you to query for multiple things on the same page safely without overwriting your previous results.

Fetching our posts

After we have specified the parameters we want and instantiated the new object, we are ready to fetch our results and display our results.

$popular_post_results = $popular_post_object->get_posts();

This should be the only method you need to call and will assign the results to the variable you specify. If you do not have Jetpack installed and activated the variable will hold a message that lets you know you need Jetpack activated. Otherwise you will receive an array back.

Displaying our posts

If you have stats available, the method will return an array of arrays, each that will have indexes of title, permalink, and post_id that hold the corresponding value for each of your popular posts. If there are no posts to display yet, it will return an empty array. For our example we will display ours in an ordered list.

if ( !empty( $popular_post_results ) ) {
    $output = '<ol>';
    foreach( $popular_post_results as $popular_post ) {
        $output .= '<li class="popular-post-' . $popular_post['post_id'] . '"><a href="' . $popular_post['permalink'] . '">' . $popular_post['title'] . '</a></li>';
    $output .= '</ol>';

    echo $output;

If all goes well, you should see a list of the top posts from your site, depending on the parameters you used. You can repeat as much as necessary and use as many times as you may need.

As long as Jetpack and this Popular Posts plugin are available and active, you can use them wherever you want in the site you are working on. This could be within your templates, in a custom widget you are creating, or even from a separate plugin. Your imagination is the limit.

Plugin zip | GitHub gist

Posted in Products We Love Snippets Tutorial and tagged with How To jetpack Plugins Tutorials

19 thoughts on Displaying Your Popular Posts Using Jetpack

Have a comment?leave a reply

Your email address will not be published.