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() . '…' ); | |
echo '<p class="more"><a href="' . get_permalink() . '" class="square-button">MORE</a></p>'; | |
echo '</div>'; | |
endwhile; | |
echo '</div>'; | |
endif; | |
wp_reset_postdata(); |
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' ); |
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 ); |
Hopefully some other developers will find a use for this.
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.
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.
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
Bill Erickson says
The issue you’re having is that BE Stats only stores data for posts by default. There’s two things you can modify.
1. The parameters sent to Jetpack (the data you receive). For instance, you might want to increase the number of popular URLs it sends back from 100 to 200. Example: https://www.billerickson.net/code/customize-be-stats-arguments/
2. Which posts you save data on. You’ll want to change this to your custom post type. Example: https://www.billerickson.net/code/be-stats-change-ranking-conditions/
Shaan Nicol says
Just wanted to say thanks for this, i’ve used this so many times on projects and have just used it again on another build and say that i never thanked you!
AKMA.me says
Maybe I can try.. But for now I just use plugin from Jetpack to display popular posts.
Coen Meerbeek says
I wonder why this wasn’t build earlier 😉 Great one Bill!