Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to be able to display on my homepage a timeline of posts from 7 of the most recent days. For example, if today is January 1, 2013, I would like my blog feed to display something that looks like the following:

  1. January 1, 2013 All the posts from this day

  2. December 31, 2012 All the posts from this day

  3. December 30, 2012 All the posts from this day

  4. December 29, 2012 All the posts from this day

  5. December 28, 2012 All the posts from this day

  6. December 27, 2012 All the posts from this day

  7. December 26, 2012 All the posts from this day

Essentially, this feed should always display 7 days worth of posts. In effect, when January 2nd comes, December 26 would be hidden and everything gets bumped by one.

This isn't outputting working quite how I would like it as when a new week comes along, it will only show 1 day worth of posts, so basically the only day that shows 7 days worth of posts is the 7th day of that week:

<section class="news-timeline">
    <?php 
        $day_check = '';
        $today = get_the_date();
        $year = date('Y');
        $week = date('W');
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'post_type'=>'post',
                'category_name' => 'whats-happening',
                'year' => $year,
                'w' => $week,
                'paged'=>$paged
            );
            $temp = $wp_query;
            $wp_query = null;
            $wp_query = new WP_Query($args);

        if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
        if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
            $day = get_the_date('j');
            if ($day == $today) {
                echo "<h3>Today</h3>";
                $counter = 0;
            } elseif ($day != $day_check) {
                echo "<h3>" . get_the_date() . "</h3>";
                $counter = 0;
            }
            if ($counter % 3 == 0) {
                $apply_css_margin = TRUE;
            }
            elseif ($counter % 3 == 1) {
                $apply_css_margin = TRUE;
            } else {
                $apply_css_margin = FALSE;
            }
            $counter++;
    ?>
    <article id="post-<?php the_ID(); ?>" <?php if($apply_css_margin) { post_class('news-timeline-margin'); } else { post_class(); } ?>>
        <a href="<?php the_permalink(); ?>">
            <div class="meta">
                <p class="author">By <?php the_author(); ?></p>
                <p class="title"><?php the_title(); ?></p>
            </div>
            <?php the_post_thumbnail('homepage-features-news'); ?>
        </a>
    </article>
    <?php $day_check = $day; endwhile; endif; ?>

    <!-- Older/Newer Pagination -->
    <?php if ($wp_query->max_num_pages > 1) : ?>
        <div class="pagination">
            <?php next_posts_link('<span class="older ss-icon">&larr;</span>previous'); ?>
            <?php previous_posts_link('Newer <span class="newer ss-icon">next</span>'); ?>
        </div>
    <?php endif; ?>
    <!-- /Older/Newer Pagination -->

    <?php
        if (function_exists('wp_pagenavi')){wp_pagenavi();}
        $wp_query = null;
        $wp_query = $temp;
        wp_reset_query();
    ?>
    <!-- /Featured Loop -->
</section>
share|improve this question
add comment

1 Answer

You can do all of this outside the template, via the pre_get_posts action and a filter on posts_where.

The first function checks if it's the main query and the home page, if so it applies a filter to the query's posts_where clause.

The posts_where function limits post selection to today's date minus 7 days, then immediately removes the filter so it isn't applied to any other queries on the page.

You can put this in your theme's functions.php file, then edit your template to remove all of the query trickery, and just run the normal loop.

function wpa85491_home_filter( $query ) {
    if ( $query->is_home() && $query->is_main_query() )
        add_filter( 'posts_where', 'wpa85491_filter_where' );
}
add_action( 'pre_get_posts', 'wpa85491_home_filter' );

function wpa85491_filter_where( $where = '' ) {
    $where .= " AND post_date > '" . date( 'Y-m-d H:i:s', strtotime( '-7 days' ) ) . "'";
    remove_filter( 'posts_where', __FUNCTION__ );
    return $where;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.