stats_get_csv (WordPress Stats) to only display top posts (not pages)

I’m trying to get the first 10 posts based on page views through WordPress Stats (now called Jetpack).
I managed to get this code to work (approximately..) after digging for hours over the forums (because variables and code changed from time to time):

        <?php
        if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv( 'postviews', "days=10&limit=10")) {
        echo '<ol class="most-viewed">';
          foreach ( $top_posts as $post ) {
            if($post['post_id'] && get_post($post['post_id']))
              echo '<li><a href="' . get_permalink( $post['post_id'] ) . '">' . 
                  get_the_title( $post['post_id'] ) . '</a> (' . number_format_i18n( $post['views']) .' visits)</li>';
          }
          echo '</ol>';
        }
        ?>

Now I’d like to exclude pages from this list and only have posts. The problem is that the WordPress.com Stats API does not offer a post_type filter. I should put somewhere this

Read More
if ( !isset($post->post_type) || $post->post_type != 'post' )

can you help me pointing out where should I add it?

Thanks in advance! 😀

Related posts

Leave a Reply

1 comment

  1. Check for the post type with the post ID:

    if ( 
        $post['post_id'] 
        && get_post( $post['post_id'] ) 
        && 'post' === get_post_type( $post['post_id'] )
    )