Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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):

        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 Stats API does not offer a post_type filter. I should put somewhere this

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

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

Thanks in advance! :D

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Check for the post type with the post ID:

if ( 
    && get_post( $post['post_id'] ) 
    && 'post' === get_post_type( $post['post_id'] )
share|improve this answer
Yup!! Works! :D – MultiformeIngegno Nov 4 '12 at 19:34
The strange thing is that if I set days=2&limit=10 I get 6 posts (instead of 10), if I set days=60&limit=10 I get 2 posts, if I set days=-1&limit=10 I get just 1 post!!! Why does this happen? :( The blog has about 2000 articles so there are of course more than 10 posts. :D – MultiformeIngegno Nov 4 '12 at 19:38
I don’t know. Sounds like a separate question. – toscho Nov 4 '12 at 19:40
Ok, sorry. Will open a new one. :) – MultiformeIngegno Nov 4 '12 at 19:43

Your Answer


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.