Display posts published one year ago - WordPress
If we have a large collection of posts, often there is a chance to ignored them by visitors because most of them only visits the freshest content.
In such situations, if we can creatively present our old posts, they help to keep our visitors to read few more posts from our blog.
The related posts, recent posts & random posts lists are consider as a effective methods in such situations.
Additional to these list, today we are going to display posts that were published over a year ago. This is a simple code snippet & we can insert it on our sidebar or single.php
file.
<?php
$current_day = date('j');
$last_year = date('Y')-1;
query_posts('day='.$current_day.'&year='.$last_year);
if (have_posts()):
while (have_posts()) : the_post();
the_title();
the_excerpt();
endwhile;
endif;
?>
This code snippet is simple to understand the logic. First it store current day in $current_day
variable, then calculate the previous year by decrementing 1 from current year & store it in $last_year
variable.
At last it do a query to retrieve posts with day & year parameter with these variable. Then list post title & excerpt on pages.
Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!
Recent Posts
- Automating Code Linting with GitHub Actions for WordPress Plugins
- Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
- The Ultimate Guide to Indexing in Database Design
- Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
- A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.