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.







Michael Kolb » Blog Archive » CyStats widget shows most popular posts
December 14th, 2008
[...] http://www.michael-kolb.co.uk/webdevelopment/cystats-getmostvisited-shows-unpublished-posts/ [...]
mk_michael
December 13th, 2008
The codesnippet refers to Cystats version 0.9.8.