by Hiroshi on November 18th, 2008
To achieve this goal, we have to make a custom SQL query by using the $wpdb object. Let’s start by creating 3 php variables:
1- Number of days between today and X days ago
2- Today’s date
3- Today’s date – X days.
<?php //To fetch posts published during the last 7 days $days = 7; //Today's date $today = date("Y-m-d H:i:s"); $daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - (7 * 24 * 60 * 60)); $result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN $daysago AND $today ORDER BY comment_count DESC LIMIT 0 , 10"); foreach ($result as $topten) { $postid = $topten->ID; $title = $topten->post_title; $commentcount = $topten->comment_count; if ($commentcount != 0) { ?> <li><a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?></a></li> <?php } } ?>
You can paste this code wherever you want, to get the 10 most commented posts from the last 7 days. To modify number of posts fetched just edit the number 10 which comes after LIMIT in result Query.



Incoming Searches