MultiformeIngegno MultiformeIngegno - 1 year ago 66
PHP Question

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

if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=-1&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

Answer Source

Solved with:

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