How to: Get posts published exactly one year ago

Here’s a nice idea to give a second life to your old posts: This code display automatically the posts you published exactly one year ago.

To achieve this recipe, simply paste the following code where you want posts published exactly one year ago to appear.

<?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;
?>

18 Responses

Nov 12 2008 22:23

Hmm, for some reason this makes loops following it fail to execute. No errors though…

Nov 13 2008 17:48

@BabyGotMac: If you use two different loops, you might need to use the rewind_posts() function.

Nov 14 2008 04:59

perfect recipe for aged blogs :)

Nov 14 2008 10:26

Excellent recipe which makes for a delicious way to showcase posts from the archive via a specified time frame.

Jun 10 2009 12:40

I will be able to use this from next on… when my blog becomes 1 year old. :-)
Thanks for this great tip and idea !

Jun 10 2009 12:41

ahem… I wanted to say “from next week on”, of course. ;)

Jul 09 2009 17:15

hi, I love this. This is exactly what I need!

I have two questions however…

1) For some reason, it’s pulling a post from November 9th instead of July 9th. Any idea why this might be? It’s supposed to be exactly one year ago to the day right? Or did I read this post wrong?

2) So if there are no posts to be displayed does the PHP call hide itself so that it doesn’t break the interface if there’s nothing to be displayed?

Dec 07 2009 12:35

@ Miles

For the question 1 : Look at the code, you have $current_day and $current_year but not $current_month. I think it’s the reason why.

for question 2 : could be nice for sure to have an echo “no post this date” but I’m not able to write code, sorry.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required