One great way to create new list of posts on your blog’s sidebar is to create a top ten list. You can create a Top ten list within the last 30 days, 6 month and 1 year. One of our past clients had the need for this feature.
The way to achieve this, we created 3 functions, listPopularPosts1month, listPopularPosts6months and listPopularPosts.
For listPopularPosts1month we added in the functions.php file the following code:
function listPopularPosts1month() {
global $wpdb;
$strBuidler = '';
$today = date("Y-m-d H:i:s"); //Today's date
$daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - (30 * 24 * 60 * 60)); //Today - 1 day
$result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts WHERE post_date BETWEEN '$daysago' AND '$today' ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postId = $post->ID;
$title = $post->post_title;
$commentCount = $post->comment_count;$thumb2 = get_post_meta($post->ID, "Thumbnail", true);
if ($commentCount != 0) {
$strBuidler .= ''; $strBuidler .= '
' . $title . ' ';
$strBuidler .= '(' . $commentCount . ')';
$strBuidler .= '';
$strBuidler .= '';
$strBuidler .= '
';
}
}return $strBuidler;
}
For listPopularPosts6months we added in the functions.php file the following code:
function listPopularPosts6months() {
global $wpdb;
$strBuidler = '';
$today = date("Y-m-d H:i:s"); //Today's date
$daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - (180 * 24 * 60 * 60)); //Today - 1 day
$result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts WHERE post_date BETWEEN '$daysago' AND '$today' ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postId = $post->ID;
$title = $post->post_title;
$commentCount = $post->comment_count;
$thumb1 = get_post_meta($post->ID, "Thumbnail", true);if ($commentCount != 0) {
$strBuidler .= ''; $strBuidler .= '
' . $title . ' ';
$strBuidler .= '(' . $commentCount . ')';
$strBuidler .= '';
$strBuidler .= '';
$strBuidler .= '
';
}
}return $strBuidler;
}
and for listPopularPosts we added :
function listPopularPosts() {
global $wpdb;
$strBuidler = '';
$result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postId = $post->ID;
$title = $post->post_title;
$commentCount = $post->comment_count;$thumb3 = get_post_meta($post->ID, "Thumbnail", true);
if ($commentCount != 0) {
$strBuidler .= ''; $strBuidler .= '
' . $title . ' ';
$strBuidler .= '(' . $commentCount . ')';
$strBuidler .= '';
$strBuidler .= '';
$strBuidler .= '
';
}
}return $strBuidler;
}
To modify the time length of the script go the the code line that creates the daysago variable like:
$daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) - (30 * 24 * 60 * 60)); //Today - 1 day
And switch 30 to the number of days you desire.
To display on your sidebar add the following to your sidebar.php file”
echo(listPopularPosts1month());
echo(listPopularPosts6months());
echo(listPopularPosts());