Many of our beginning readers soon start tweaking their WordPress themes, so we've got a WordPress cheat sheet to help you 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 that would display posts from the 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 the previous week's posts, let's first see how you can display the current week's posts using WP_Query. Copy and paste the following code into your theme's functions.php file or a site-specific plugin.
función wpb_this_week () $ week = date ('W'); $ año = fecha ('Y'); $ the_query = new WP_Query ('year ='. $ year. '& w ='. $ week); if ($ the_query-> have_posts ()): while ($ the_query-> have_posts ()): $ the_query-> the_post (); ?>In the example code above, we first find out the current week and year. We then use those values in WP_Query to display the posts for the current week. Now all you need to do is add
in your theme file where you want to display the posts.
This was simple, right? Now to display the posts from the past week, all you need to do is minus 1 from the week value. But if this is the first week of the year, then you will get 0 for the current week and year instead of last year. Here is how to fix that problem.
función wpb_last_week_posts () $ thisweek = date ('W'); si ($ thisweek! = 1): $ lastweek = $ thisweek - 1; else: $ lastweek = 52; terminara si; $ año = fecha ('Y'); if ($ lastweek! = 52): $ year = date ('Y'); else: $ año = fecha ('Y') -1; terminara si; $ the_query = new WP_Query ('year ='. $ year. '& w ='. $ lastweek); if ($ the_query-> have_posts ()): while ($ the_query-> have_posts ()): $ the_query-> the_post (); ?>In the example code above we have placed two checks. The first check sets the value of the last week to 52 (which is the last week of a year) when the value of the current week is 1. The second check sets the value of the year to the past year when the value of the last week is 52.
To display posts from the past week, all you need to do is add
to your theme's template file where you'd like to display them. Or if you want to have a shortcode so you can add it to a page or a widget, just add this line below the code given above.
add_shortcode ('lastweek', 'wpb_last_week_posts');Now you can use this shortcode in a post, page or widget like this:
[la semana pasada]
Note that you don't always need WP_Query to create custom queries. WordPress includes several features to help you display recent posts, archives, comments, etc. If there's an easier way to use existing functions, then you don't really need to write your own queries.
We hope this article has helped you display last week's posts in WordPress. Experiment with the code and modify it to suit your needs. Let us know if you have any questions by leaving a comment below or join us on Twitter.