1

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

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

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

Thanks in advance! :D

2

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'] )
)
  • 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. – fuxia Nov 4 '12 at 19:40
  • Ok, sorry. Will open a new one. :) – MultiformeIngegno Nov 4 '12 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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