Hi,
I am using Wordpress Popular Posts.
I have modified the plugin heavily to make it work with my site.
Currently on the homepage of my site it shows the most recent posts by day, week and month
The problem is at the beginning of each new day there are no popular posts for the day for the first few hours, so it breaks my layout.
if ( !is_array($mostpopular) || empty($mostpopular) ) {
$content .= "<p><a href=\"/videos\">".__('Looking for videos? Click here', 'wordpress-popular-posts')."</a></p>"."\n";
}
The plugin writer gave me this code below to display the week's popular posts in place of the day's popular posts if there was none for the day. The code just showed up blank.
if ( !is_array($mostpopular) || empty($mostpopular) ) {
$mostpopular = $wpdb->get_results("SELECT $wpdb->posts.ID, $wpdb->posts.post_title $fields FROM $wpdb->posts RIGHT JOIN ".$table."cache ON $wpdb->posts.ID = ".$table."cache.id WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_password = '' AND ".$table."cache.day >= '".gmdate("Y-m-d")."' - INTERVAL 7 DAY AND ".$table.".pageviews > 0 $nopages $exclude GROUP BY $wpdb->posts.ID ORDER BY $sortby DESC LIMIT " . $instance['limit'] . "");
}
If there are no popular posts for the day, I want the popular posts for the week to show up.
PM me if you want the full plugin code
Arnav Joy answers:
please tell me what is the result of
print_r($mostpopular);
please paste the result here
Arnav Joy comments:
or you can use it as
foreach ( $mostpopular as $mp)
{
?>
<a href="<?php echo $mp->guid;?>"><?php echo $mp->post_title;?></a>
<?php } ?>
ag123 comments:
Array ( )
that was all that came out when I put in
print_r($mostpopular);
John Cotton answers:
Try this:
if ( !is_array($mostpopular) || empty($mostpopular) ) {
$mostpopular = $wpdb->get_results("SELECT $wpdb->posts.ID, $wpdb->posts.post_title $fields FROM $wpdb->posts RIGHT JOIN {$table}cache ON
$wpdb->posts.ID = {$table}cache.id
WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_password = ''
AND {$table}cache.day >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND {$table}pageviews > 0 $nopages $exclude
GROUP BY $wpdb->posts.ID ORDER BY $sortby DESC LIMIT " . $instance['limit'] . ");
}
ag123 comments:
Hi,
I got this error code when I used this code
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
Full plugin
[[LINK href="http://pastebin.com/e25TfrtF"]][[/LINK]]
John Cotton comments:
Looking at that plugin code, it seems you've cut and pasted with some odd CR/LFs.
Try this:
if ( !is_array($mostpopular) || empty($mostpopular) ) {
$mostpopular = $wpdb->get_results("SELECT $wpdb->posts.ID, $wpdb->posts.post_title $fields FROM $wpdb->posts RIGHT JOIN {$table}cache ON $wpdb->posts.ID = {$table}cache.id WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_password = '' AND {$table}cache.day >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND {$table}pageviews > 0 $nopages $exclude GROUP BY $wpdb->posts.ID ORDER BY $sortby DESC LIMIT " . $instance['limit'] );
}