I have a simple question. A really simple question to be honest but I can't figure it out, no matter how often I go to wordpress.org and read up on their docs.
All I want is to display the most commented posts within the past week (i.e. Mon-Sun) or the past 7 days (whichever is easier).
I tried doing this already with a query within the loop but for some reason it's showing the most commented posts of all time even though I added some parameters to the query to specify the range of posts that I wanted to have covered.
Can someone take a look at the code below and tell me what to change?
<ul>
<?php
$week = date('W');
$year = date('Y');
$popular = new WP_Query('orderby=comment_count&posts_per_page=5&w=$week&year=$year'); ?>
<?php while ($popular->have_posts()) : $popular->the_post(); ?>
<li>
<?php $justanimage = get_post_meta($post->ID, 'image', true);
if ($justanimage) {
?>
<img align="left" src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "image", true); ?>&w=75&h=63&zc=1" alt="<?php the_title(); ?>" />
<?php } else { ?>
<img align="left" src="<?php bloginfo('template_directory'); ?>/images/no-thumb.png" width="75px" height="63px" alt="<?php the_title(); ?>" />
<?php } ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</li>
<?php endwhile; ?>
</ul>
Thanks,
Brennen
Erez S answers:
Replace:
$popular = new WP_Query('orderby=comment_count&posts_per_page=5&w=$week&year=$year'); ?>
With:
$popular = new WP_Query('orderby=comment_count&posts_per_page=5&w='.$week.'&year='.$year.''); ?>
Enjoy
Erez S comments:
Or you can use this for the last 7 days:
//based on Austin Matzko's code from wp-hackers email list
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
Oleg Butuzov answers:
something like this =)
<?php
global $wpdb;
$mostCommented = $wpdb->get_results("SELECT ID, post_title
FROM ".$wpdb->posts." WHERE
`post_status` = 'publish'
AND `post_type` = 'post'
AND `post_date` >= (SELECT date_SUB(MAX(post_date), interval 8 DAY)
FROM ".$wpdb->posts." WHERE `post_status` = 'publish' AND `post_type` = 'post'
)
ORDER BY comment_count DESC
LIMIT 5");
?>
<ul>
<?php foreach($mostCommented as $item){ ?>
<li>
<?php if (get_post_meta($item->ID, 'image', true)) { ?>
<img align="left" src="<?php bloginfo('template_directory');
?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "image", true);
?>&w=75&h=63&zc=1" alt="<?php echo apply_filters('the_title', $item->post_title); ?>" />
<?php } else { ?>
<img align="left" src="<?php bloginfo('template_directory');
?>/images/no-thumb.png" width="75px" height="63px" alt="<?php
echo apply_filters('the_title', $item->post_title); ?>" />
<?php } ?>
<h2><a href="<?php echo get_permalink( $item->ID ); ?>">
<?php echo apply_filters('the_title', $item->post_title); ?>
</a></h2>
</li>
<?php } ?>
</ul>
simple and fast
Utkarsh Kukreti answers:
Use
$popular = new WP_Query("orderby=comment_count&posts_per_page=5&w=$week&year=$year");
Variables aren't parsed when in single quotes.
Brennen Jones comments:
Thanks for that. I didn't see your message until it was too late. I was responded to the messages through my email and got to yours after I got to the answer I chose. Sorry. :-(
But ur solution was the simplest of them all. I knew it was a simple question, lol.
Ipstenu answers:
In theory, this plugin should be able to do the heavy lifting for you:
http://wordpress.org/extend/plugins/popularity-contest
You set it up so you only use comment weight, and then use the above code to parse for the last 7 days.