How to Display Last Week’s Posts in WordPress

Many of our beginner level readers soon start modifying their WordPress themes thats why we have a WordPress theme cheat sheet to help them get started. This brings some interesting challenges for new users. One such reader, recently asked us how to display last week’s posts in WordPress. They just wanted to add a section on their home page which displayed posts from previous week. In this article, we will show you how to display last week’s posts in WordPress. Before we show you how to display previous week’s posts, let’s first take a look at how you can display current week’s posts using WP_Query. Copy and paste the following code in your theme’s functions.php file or a site-specific plugin.

function wpb_this_week() { 
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) : 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h2>
	<?php the_excerpt(); ?>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>
<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
}

  In the example code above, we first found out the current week and year. We then used those values in WP_Query to display posts from current week. Now all you need to do is add <?php wpb_this_week(); ?> in your theme file where you want to display the posts. This was simple, wasn’t it? Now to display last week’s posts all you need to do is minus 1 from the week’s value. But if this is the first week of the year, then you will get 0 for the week and current year instead of last year. Here is how you fix that problem.

function wpb_last_week_posts() { 
$thisweek = date('W');
if ($thisweek != 1) :
$lastweek = $thisweek - 1;   
else : 
$lastweek = 52;
endif; 
$year = date('Y');
if ($lastweek != 52) :
$year = date('Y');
else: 
$year = date('Y') -1; 
endif;
$the_query = new WP_Query( 'year=' . $year . '&w=' . $lastweek );
if ( $the_query->have_posts() ) : 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h2>
	<?php the_excerpt(); ?>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>
<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;

}

  In the sample code above we have placed two checks. The first check sets the last week’s value to 52 (which is the last week in a year) when the current week’s value is 1. The second check sets year’s value to last year when the last week’s value is 52. To display last week’s posts all you need to do is add <?php wpb_last_week_posts(); ?> to your theme’s template file where you would like to display them. Or if you would like to have a shortcode so that you can add this into a page or a widget, then simply add this line below the code given above.

add_shortcode('lastweek', 'wpb_last_week_posts');

  You can now use this shortcode in a post, page, or a widget like this: [lastweek] Please note, that you don’t always need WP_Query to create custom queries. WordPress comes with a handful of functions to help you display recent posts, archives, comments, etc. If there is an easier way to use the existing functions, then you don’t really need to write your own queries. We hope this article helped you display last week’s posts in WordPress. Experiment with the code and modify it to meet your needs. Let us know if you have any questions by leaving a comment below or join us on Twitter.

Rate this post