tags:

views:

769

answers:

1

I would like to show only the posts of the last X day's on my wordpress frontpage. Let's say, a week, so 7 day's.

What's the propper way to tell wordpress to select only the posts in the last X day's in the loop?

Currently, I've got it working trough a hack, but it messes up the pagination. Going to the next page does not select the right posts.

//Hack found on the bottom of http://codex.wordpress.org/Template_Tags/query_posts
function filter_where($where = '') {
    //posts in the last 7 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
if ( have_posts() ) : while ( have_posts() ) : the_post();
... and the usual
+1  A: 

You may partial success with the following:

function filter_where($where = '') {
  $date_split = date('Y-m-d', strtotime('-7 days'));
  if (is_paged()) {
    $where .= " AND post_date < '" . $date_split . "'";
  } else {
    $where .= " AND post_date > '" . $date_split . "'";
  }
  return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);

My home page shows posts in the last 7 days, page/1 starts showing posts after that day and page/2 works as a continuation of page/1 as expected.

David Carrington