Uly.me

cloud engineer

  • Home
  • About
  • Archives
Home/PHP/Display Post From A Year Ago

June 11, 2014

Display Post From A Year Ago

Here’s a cool little WordPress script to display a post from a year ago. You can display your old content to entice your readers to view older articles. You can display your old posts on your theme’s sidebar, after your post content, or just anywhere on your theme. Here’s the code.

// current day of the month without leading zeros, e.g. 1-31.
$current_day = date('j');
// last year
$last_year = date('Y')-1;
// perform a query based on today's date last year
query_posts('day='.$current_day.'&year='.$last_year);
if (have_posts()):
    while (have_posts()) : the_post();
       the_title();
       the_excerpt();
    endwhile;
endif;

// current day of the month without leading zeros, e.g. 1-31. $current_day = date('j'); // last year $last_year = date('Y')-1; // perform a query based on today's date last year query_posts('day='.$current_day.'&year='.$last_year); if (have_posts()): while (have_posts()) : the_post(); the_title(); the_excerpt(); endwhile; endif;

The result will depend if you have a post last year on the exact date as today, otherwise you’ll get a blank result. I suppose to be on the safe side, you can query all posts of the current month last year. Your chances of getting results is much better. You can use the date(‘n’) function to extract the current month. Your query would look something like this:

$current_month = date('n');
query_posts('month='.$current_month.'&year='.$last_year);

$current_month = date('n'); query_posts('month='.$current_month.'&year='.$last_year);

Filed Under: PHP, WP Tagged With: last year

Have content delivered to your mail. Subscribe below.

About Me

I'm Ulysses, Cloud Engineer at Cardinal Health based in Columbus. This blog is about Linux and Cloud technology. When off the grid, I enjoy riding my electric skateboard. I've surfed, snowboarded and played the saxophone in the past. I hope you find this website helpful. It's powered by WordPress and hosted on AWS LightSail.

  • Cloud
  • Linux
  • Git

Copyright © 2012–2021