Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Please share of which software the function stats_get_csv() is part of. That will help others to answer your question. –  hakre Mar 16 '11 at 11:36
    
@hakre I believe it's WP.com Stats - wordpress.org/extend/plugins/stats –  TheDeadMedic Mar 16 '11 at 15:27
    
Yes, it is the Wordpress.com Stats plugin. –  jwp Mar 16 '11 at 17:05

3 Answers 3

The API returns the following columns when you query the postviews table:

  • date
  • post_id
  • post_title
  • post_permalink
  • views

For my blog the post_id column was either empty or 0 (for the homepage). So unless you have good values there, you will have to work from the post_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.

share|improve this answer

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_ids to figure out which ones have a post_type of post. In your loop you can use something like this:

$post = get_post($p['post_id']);
if ( !isset($post->post_type) || $post->post_type != 'post' )
    continue;
if ( has_post_thumbnail( $post->ID ) )
    $thumbnail = get_the_post_thumbnail( $post->ID );
else
    $thumbnail = '';
share|improve this answer

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:

 $postTitle = $single_post['post_title'];
    if($postTitle == 'Home page') {
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.