CyStats getMostVisited() shows unpublished posts

December 12, 2008 - Views: 1649
Category: Web Development

CyStats is one of the most and best statistic plugins for wordpress. I use this plugin in my wordpress blog too.

I noticed that this API function shows unpublished posts in sidebar too, which is mostly not wanted. To avoid this, I’ve changed a little inside the CyStats core.

Open the file: wp-content/plugins/cystats/includes/template-functions.php and do the programming in my code comments.


function cystats_getMostVisited($limit, $pre, $pos, $showmode=TRUE)
{
  global $wpdb;
  $ret = array();
  $q = "SELEC val1 AS item val3 AS value
        FROM ".$wpdb->prefix.CYSTATS_TABLE_STATISTICS."
        WHERE type=".CYSTATS_POSTCOUNT."
        ORDER BY val3 DESC
        LIMIT ".$wpdb->escape($limit);
  $r = $wpdb->get_results($q,ARRAY_A);

  if(is_array($r))
  {
    foreach ($r AS $row)
    {
      #$percent = round(($row['value']/$max * 100));
      // [mk] CHANGE SELECT STATEMENT
      // [mk] ADD - WHERE post_status='publish'
      $postdata=$wpdb->get_results
("SELECT post_title,guid FROM ".$wpdb->posts." WHERE
post_status='publish' AND ID=".(intval($row['item'])),ARRAY_A);
      // [mk] ADD IF STATEMENT
      if ($postdata[0]['guid'] != "")
      {
        $row['item']='<a href="'.$postdata[0]['guid'].'">'.
str_replace(" "," ",$postdata[0]['post_title']).'</a>';
        $ret[] = $pre.$row['item'].' ('.$row['value'].')'.$pos;
      }
    }
    if($showmode==TRUE)
    {
      foreach($ret AS $row)
      {
        echo $row;
      }
    }
    else
    {
      return($ret);
    }
  }
  else
    return FALSE;
}

Adapt the sql query and limit it to all posts with status “publish”. Secondary add the “if” statement around the two lines, because the main foreach loops around the very fist query.

Alternatively, you may try to change the first “$q” query, which is more hard work.

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • MisterWong
  • Yigg
  • Technorati
  • Blogosphere News
  • LinkArena
  • Webnews.de

Tags: ,

Related Posts:

2 Responses

  1. The codesnippet refers to Cystats version 0.9.8.

Leave a Reply

Home | Imprint | Impressum | Archiv
© 2008 M.Sc. Dipl.Inf. Michael Kolb. Powered by Wordpress. HTML CSS
my technorati | my blogcatalog