This article will show you an advanced usage of powerful query_posts() function in WordPress to retrieve popular posts in a period of time (current week, month, year or in last 30 days) by comment count. Using the query_posts()
function instead of raw MySQL query is always recommended for all WordPress users.
First, let's look at the code of showing popular posts by comment count in all time, it looks like this:
<ul> <?php query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; wp_reset_query(); ?> </ul>
This will query for 10 first posts with greatest comment count. Just paste this code to any place of your theme to show the popular posts.
Now, to get popular posts in current week, we must add more parameters to the query string. The code is:
<ul> <?php $week = date('W'); $year = date('Y'); query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=' . $year . '&w=' . $week); while (have_posts()): the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; wp_reset_query(); ?> </ul>
Here we use the PHP date()
function to get the current week and year and push them to the query string.
Similarly, the code for getting popular posts in current month is:
<ul> <?php $month = date('m'); $year = date('Y'); query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC&year=' . $year . '&monthnum=' . $month); while (have_posts()): the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; wp_reset_query(); ?> </ul>
Getting popular posts in last 30 days is a bit more complicated. We have to tweak the query string, too, but in a different way. Here it is:
<ul> <?php function filter_where($where = '') { //posts in the last 30 days $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC'); while (have_posts()): the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr('Permalink to %s'), the_title_attribute('echo=0')); ?>" rel="bookmark"><?php the_title(); ?></a></li> <?php endwhile; wp_reset_query(); ?> </ul>
As you see, the query string is still the same, but the difference is the WHERE clause. We have added a condition for getting only posts in last 30 days. You can change "30 days" to any thing you want (like "1 year", "7 days", etc).
The query_posts()
function is very powerful to get posts with some conditions. You can see all parameters at the Codex, and combine them to get more complicated conditions.
Leave a Reply