Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

8 CommentsLeave a Reply

  1. Ali Hesari

    This is very useful for me. Thank you.

  2. hitesh

    this is very useful for us. keep it

  3. Kenny

    This would be great if a YouTube video was made available

  4. Allison

    I am curious about when/why I would want to display the week’s posts? On my home page, I display excerpts, so the posts from my past 2 weeks or so are visible. What am I missing here? Thanks!

    • WPBeginner Support

      You don’t need to. But some other website owners may have a layout where they might want to display previous week’s posts separately. This tutorial is aimed to help those users.

      Admin

  5. faisal adil

    Awesome :)

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.