Displaying Popular Posts using BE-Stats

Jetpack is a great plugin for collecting stats on your posts. But the only frontend display of that data they provide is the Top Pages and Posts widget. What if you want to filter that down to a more refined list?

BE Stats is the plugin for you. It asks for the 100 most popular posts over the past 30 days. I filter this list down to just posts and then store its ranking as post meta (‘be_stats’).

Download BE-Stats from WordPress.org

You can then write custom queries using that metadata, or use the Display Posts Shortcode which makes it even easier.

Here’s some examples using the shortcode:


Lists the 10 most popular posts

Lists the 4 most popular posts

Lists the 4 most popular posts tagged “basic”

I developed this for a recent client project (still under development, will share link once it is live). The website is a news site that releases a lot of content. On the home and category archives they wanted to display recent posts and popular posts from that section. Here’s what I placed where I want popular posts:

<?php
// Popular Posts
$args = array(
'meta_key' => 'be_stats',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => 3,
'cat' => get_query_var( 'cat' ),
'post__not_in' => $used_posts,
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
echo '<div class="popular-posts-listing">';
echo '<h4>Most Popular ' . $category->name . ' Posts</h4>';
while( $loop->have_posts() ): $loop->the_post();
echo '<div class="post">';
if( has_post_thumbnail() )
echo '<a href="' . get_permalink() . '">' . be_get_post_thumbnail( null, 134, 134, true ) . '</a>';
echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
echo wpautop( get_the_excerpt() . '&hellip;' );
echo '<p class="more"><a href="' . get_permalink() . '" class="square-button">MORE</a></p>';
echo '</div>';
endwhile;
echo '</div>';
endif;
wp_reset_postdata();
view raw functions.php hosted with ❤ by GitHub

There’s also some filters for you to customize how the plugin works.

be_stats_args – Controls what arguments are passed to WordPress.com Stats API. Example:

<?php
/**
* Customize BE Stats Arguments
* This will limit it to 7 days of pageviews (default is 30) and 20 posts (default is 100)
*
* @author Bill Erickson
* @link http://www.billerickson.net/code/customize-be-stats-arguments
*
* @param array $args
* @return array $args
*/
function be_customize_stats_args( $args ) {
$args['days'] = 7;
$args['limit'] = 20;
return $args;
}
add_filter( 'be_stats_args', 'be_customize_stats_args' );
view raw functions.php hosted with ❤ by GitHub

be_stats_update – Conditional for determining if stats data should be saved Examples:

<?php
// Save all stats data, don't limit it to just posts
add_filter( 'be_stats_update', '__return_true' );
// -- OR --
/**
* Limit Stats to Events
*
*/
function be_stats_for_events( $update, $id ) {
return 'events' == get_post_type( $id );
}
add_filter( 'be_stats_update', 'be_stats_for_events', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

Hopefully some other developers will find a use for this.

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Richard Buff says

    This is great stuff, Bill. A little off-topic, but I can’t help but wonder: Jetpack has a bunch of available features. Which ones do you actually use on projects other than the stats?

    • Bill Erickson says

      I disable everything but Stats, Notifications, Subscriptions, Spelling and Grammar, Custom CSS, and Extra Sidebar Widgets.

      I also install Manual Control for Jetpack so that when new modules come out they aren’t auto-updated.

  2. John Garner says

    Hi Bill,

    Thanks for sharing. I had come across your site previously so when I saw your name again in the Google, I jumped straight to your post in the results.

    I find that Jetpack inserts lots of code that I don’t want and don’t use and even found it hard to remove some of the CSS and Javascript of the code. The usual deregister for styles and scripts just didn’t seem to work and does for most scripts that use enqueue…

    Do you not find that even when you disable things (great to know about the Manual Control plugin btw, thanks) that a lot of code you are not actually using is still being inserted in the pages or did I get the wrong impression? Feels like an elephant of a plugin now, to me…

    I actually switched to Google Analytics, removed Jetpack and used a simple Newsletter plugin with all else disabled apart from Akismet.

    Any thoughts?

    • Bill Erickson says

      I agree, I’m not a big fan of Jetpack. But it does have a great API for the stats, which is why I built this using Jetpack and not Google Analytics. The only time I use Jetpack is when we will be collecting stats like this.

  3. Shaan Nicol says

    I’m having trouble bringing up stats for a custom post type, can’t work out where i’ve gone wrong here the page is just displaying nothing. If i remove the ‘meta_key’ => ‘be_stats’ it works fine