I recently learned that if you have WordPress.com Stats installed, you can take advantage of stats_get_csv()
(Part of the WordPress.com Stats Plugin).
<?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=-1&limit=4') ) : ?>
<ol>
<?php foreach ( $top_posts as $p ) : ?>
<li><a href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a></li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
That’s how I have my code set up now. The problem is that it shows pages as well as posts. Also, I want to add the featured thumbnail next to each item as well as an array of custom post types. Is this possible? If so, can someone help me out?
The API returns the following columns when you query the
postviews
table:For my blog the
post_id
column was either empty or0
(for the homepage). So unless you have good values there, you will have to work from thepost_permalink
value and determine whether it is a page or a post (via a query on the database or a regex on the URL?), and then query your database for the post thumbnail, because the WordPress.com stats API does not collect info on that.The WordPress.com Stats API does not currently offer a post_type filter. You can implement one yourself in one of a number of ways. The basic idea is to loop over the
post_id
s to figure out which ones have apost_type
ofpost
. In your loop you can use something like this:As to the excluding pages, if you have only a few pages, you can do it as described here:
Inside your foreach loop, before the <li> add: