We plan on adding a popular posts widget in a future update, but for now, here are a few ways you can make one yourself.
Independent Analytics stores the number of views each page has in a meta field called iawp_total_views
. This makes it possible to sort your posts by this field in order to get a list of posts sorted by popularity.
You can use this meta field with a plugin or in a solution you code yourself.
Using a plugin
- Ultimate Posts Widget – this is a good option if you want a classic widget. Order the posts by a custom field and then enter
iawp_total_views
as the field to sort by. - Advanced Query Loop – this plugin provides a more customizable version of the built-in Query Loop block. Choose this plugin if you are using the block editor and want to list your popular posts inside the post/page content.
Coding it yourself
There are two different approaches to programming the widget.
Using iawp_top_posts()
The developer API has a function called iawp_top_posts()
that is made for getting popular posts. You can choose any post type and decide the exact date range to use. For instance, you could use this function to get the most popular posts from the past 30 days or from last month.
Here’s an example of how you can create a simple list of popular posts using this function:
$posts = iawp_top_posts([
'post_type' => 'posts',
'limit' => 10,
'from' => new DateTime('-30 days'),
'to' => new DateTime('now'),
'sort_by' => 'visitors'
]);
<div>
<ul>
<?php foreach($posts as $post) {
echo '<li>';
echo '<a href="'. get_permalink($post->id) .'">'. $post->title .'</a>';
echo ' ('. $post->views .' Views)';
echo '</li>';
} ?>
</ul>
</div>
Using iawp_total_views
Since each page has its total view count stored in iawp_total_views
, you can use it with WP_Query
to get an ordered list of posts. For example, here’s an example of how to get a list of the 10 most popular posts and output the title of each one:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'meta_key' => 'iawp_total_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>Views: ' . get_post_meta(get_the_ID(), 'iawp_total_views', true) . '</p>';
}
}
wp_reset_postdata();
If you’re already familiar with using WP_Query
and post meta fields, then this might be the best option for your site. The trade-off is that it doesn’t let you specify a date range, so you can only find the all-time most-viewed pages this way.
Next steps
If you are developing your own plugin or child theme, then you can wrap either code snippet in your own function or shortcode to output wherever needed.
Otherwise, you can put the code snippet into a plugin like WPCode, which can also create a shortcode for you. Then, you can add the shortcode anywhere you want to see the list of popular posts.
Learn how to use Independent Analytics in bite-sized email lessons delivered every 1-3 days.
Signup Here