WordPress.org

Ready to get started?Download WordPress

Forums

Select only the posts in the last X day's in the loop (1 post)

  1. zinruss
    Member
    Posted 2 years ago #

    Greeting.

    I need a big favor from you guys. I would like to show only the posts of the last 7 day's on frontpage. I mean page 1 show the last 7 days, page 2 show day 8-14 and so on.

    I search for a few hours and finally found something that may do the trick.

    http://stackoverflow.com/questions/1390239/wordpress-loop-only-include-posts-x-days-old

    The filter provided by David Carrington help me but i have a big problem. The code only show the last 7 days posts on frontpage without pagination. Am i missing something here?

    Below is the code that i'm using

    <?php
    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);
    ?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    MY CONTENT HERE
    
    <?php endwhile; ?>
    <?php remove_filter('posts_where', 'filter_where'); ?>
    <?php endif; ?>
    
    <?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?>

    Please help me. Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic