WordPress: Fixing the broken links from Jetpack top stats

I use the upgraded awesome plugin Jetpack by WordPress.com to show the ‘Top Posts’ is the sidebar. I recently noticed that the links for these post were broken, even though I wasn’t doing anything wrong at all. I queried the plugin for top 5 posts using:

$top_posts = stats_get_csv('postviews', 'days=-1&limit=6&summarize');
echo '

    ';
    foreach ( $top_posts as $p ) {
    if($p['post_id'])
    echo '

  • '. $p['post_title'] .' - '. $p['views'] . ' views
  • ';
    }
    echo '

';

But somehow, the value at post_permalink key was wrong. Instead of pulling out my hair, I downloaded the Jetpack plugin’s source code, opened the file stats.php and copied a few lines and now the following works awesomely well.


if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=-1&limit=6&summarize')) {
echo '

';
}

I hope this helps people looking for a quick fix to this problem.

2 thoughts on “WordPress: Fixing the broken links from Jetpack top stats

  1. Jordan

    Thanks for posting this! It helped me fix my website. The important part is using get_permalink(), get_the_title(), and number_format_i18n(). Also, your second snippet has the variable $post appear out of nowhere. I think you meant $p.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *