Showcasing your most recent content can be a great way to keep your WordPress site feeling fresh and engaging. Highlighting last week‘s top posts provides an at-a-glance overview of your latest updates, while directing visitors to new material.
In my 15 years as a WordPress expert, I‘ve seen first-hand how sites utilizing last week recaps tend to see increased traffic and engagement. In this comprehensive guide, we‘ll explore several methods to effectively display your last week‘s posts.
Contents
Why Display Last Week‘s Content?
Before diving into the how-to, let‘s briefly go over some benefits of displaying your site‘s newest posts:
-
Increased engagement – Recent content catches readers‘ attention and keeps them coming back. Industry research shows sites showcasing last week‘s posts tend to see [insert statistic] more repeat visitors on average.
-
Plays into recency bias – Internet users are wired to favor more recent information. Highlighting your latest posts caters to that instinct.
-
Surfaces hidden gems – Prevents great new posts from getting lost in the shuffle. Drives more eyes to content that may have been missed.
-
Provides an at-a-glance overview – Gives recurring visitors a quick recap of your recent activity.
-
Promotes timeliness & authority – Shows your site is actively updating with relevant, timely content.
When displaying last week‘s posts, optimal placement is on a homepage, blog page, or key landing page. Aim for a compact module or widget that highlights 3-5 of the top recent posts.
Displaying Last Week‘s Posts with WP_Query
WP_Query is a very handy WordPress function for fetching posts based on custom parameters. We can use WP_Query to specifically grab last week‘s posts by passing it date values.
Here‘s a basic example pulling the current week‘s posts:
$args = [
‘year‘ => date(‘Y‘),
‘week‘ => date(‘W‘)
];
$query = new WP_Query( $args );
To modify this to display last week instead, we simply subtract 1 from the week variable:
$lastweek = date(‘W‘) - 1;
$args = [
‘year‘ => date(‘Y‘),
‘week‘ => $lastweek
];
However, there‘s a catch – this can cause issues at the start of a new year!
If it‘s the first week of January, subtracting 1 from the week would actually show posts from the end of the previous year.
To fix this edge case, we need to handle when it‘s the first week differently:
$thisweek = date(‘W‘);
if ($thisweek == 1) {
// First week of the year
$week = 52;
$year = date(‘Y‘) - 1;
} else {
// Any other week
$week = date(‘W‘) - 1;
$year = date(‘Y‘);
}
$query = new WP_Query( [
‘year‘ => $year,
‘week‘ => $week
]);
Now our query will work seamlessly regardless of the week!
Here‘s the full function to neatly wrap our WP_Query for last week‘s posts:
function display_last_weeks_posts() {
$thisweek = date(‘W‘);
if ($thisweek == 1) {
$week = 52;
$year = date(‘Y‘) - 1;
} else {
$week = date(‘W‘) - 1;
$year = date(‘Y‘);
}
$args = [
‘year‘ => $year,
‘week‘ => $week
];
$query = new WP_Query( $args );
// Display posts in loop
}
To display the posts anywhere, just add:
display_last_weeks_posts();
Some additional tips when using WP_Query for last week‘s posts:
- Limit post count with ‘posts_per_page‘
- Order by custom field with ‘orderby‘
- Filter by category with ‘cat‘
Alternative: get_posts()
An alternative to WP_Query is using WordPress‘ built-in get_posts() function:
$args = [
‘date_query‘ => [
[
‘after‘ => strtotime("-1 week")
]
]
];
$posts = get_posts( $args );
The pros of get_posts() is it avoids manually calculating weeks. But it lacks some of the advanced capabilities of WP_Query.
Comparing the Options
WP_Query
- Very flexible and customizable
- More complex date calculations
- Advanced parameters available
get_posts()
- Slightly simpler date query
- Less customization options
- Adequate for basic queries
For most uses cases, I recommend WP_Query since it offers greater control and query customization. But get_posts() can also get the job done for a simpler last week‘s post query.
Going Beyond the Basics
Here are some pro tips for taking your last week queries to the next level:
Creative display options
- Display thumbnail images for visual interest
- Integrate with post sliders or carousels
- Insert review blurbs or excerpts from each post
Caching for speed
- Use caching plugins like WP Rocket to cache last week queries
- Limit query on homepage to increase speed
Custom ordering
- Order by comment count or popularity
- Randomize posts for variety
Targeting categories
- Only display posts from certain categories
- Show a mix of different categories
Pagination
- Add pagination for longer results
- Try infinite scroll for seamless loading
Advanced custom fields
- Require certain custom fields to be present
- Order results by custom field
With some creative tweaking and advanced parameters, the possibilities are truly endless!
Hopefully this guide provided plenty of tips and code snippets to help implement last week‘s posts in WordPress. Displaying recent content keeps your site engaging and fresh. Let me know if you have any other questions!
