On this day PHP code

I’m working with this code snippet for an “on this day” type post. However it is only showing 1 year in the past. I’m looking to automate this so it shows on this day in 2009, 2010, 2011… and as the years advance, keep these years showing. With the way it’s written as a -1, -2, it will be a dynamic “year ago” rather than a static “in 2009”.

Is there a way to automate this to go back 2, 3 4 years without having to duplicate the code for (-2, -3, -4, etc)?

function on_this_day() {
$on_this_day = get_posts('year='.(date('Y')-1).'&monthnum='.date('n').'&day='.date('j'));
if( $on_this_day ) {
    echo '<a class="on-this-day" href="'.get_day_link( (date('Y')-1), date('m'), date('d')).'" title="On This Day in '.(date('Y')-1).'">';
    echo 'On this day in '.(date('Y')-1);
    echo '</a>';
} else {
    echo '<span class="on-this-day-none">'.'On this day in <span>'.(date('Y')-1).'</span></span>';
}

Related posts

Leave a Reply

2 comments

  1. Adapted from the Codex, tweaking WP_Query to get all years before this.

    Untested, but should work.

    $on_this_day = array(
        // remove the year from the first query array
        'monthnum' => date('n'),
        'day' => date('j');
    );
    
    
    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
        // posts for all years before this year
        $where .= " AND post_date < " . date('Y');
        return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $on_this_day );
    remove_filter( 'posts_where', 'filter_where' );
    
    if( $query->have_posts() ) : while ($query->have_posts()) : $query->the_post();
        $year = get_the_time('Y'); 
        $month = get_the_time('m'); 
        $day = get_the_time('d');            
        echo '<a class="on-this-day" href="'.get_day_link($year, $month, $day).'" title="On This Day in '.$year.'">';
        echo 'On this day in '.$year;
        echo '</a>';
    
    endwhile;
    endif;
    

    UPDATE
    Updated code with the complete loop. It should now output a list of links for the years past, leaving out the years with no posts for that date;

  2. I took the code provided by the answer from @moraleida and improved it so that it would look at the post dates, and not the server dates.

    I also modified it so that it would look for all posts on that post Month and Day, and link to them as long as it’s not the same post ID that’s being displayed on. Afterall, since you’re already reading the post from Jan 31, 2012, no need to link to itself. For example, the 2012 post should show a 2011 link, and a 2011 post should show the 2012 link. This is also beneficial if you have posted more than 1 time per day. It will show both posts on that day, even if it’s from the same year.

    I turned this into a function which can go into functions.php. To display this in your template, use <?php on_this_day(); ?> in your template file.

    function on_this_day(){
    $on_this_day = array(
        // Build the array using post date, and omit the year
        'monthnum' => get_the_date('n'),
        'day' => get_the_date('j')
    );
    
    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $on_this_day );
    remove_filter( 'posts_where', 'filter_where' );
    
        if( $query->have_posts() ) {
            // Uncomment this next line to echo an On this day prefix to the years.
            // echo "On this day in ";
            $postYear = get_the_date('Y');
    
            while ($query->have_posts()) {
                $query->the_post();
                $year = get_the_time('Y'); 
                $queryYear = get_the_date('Y');
                $month = get_the_time('m'); 
                $day = get_the_time('d');  
                if ($queryYear == $postYear) { 
                    // I use permalinks to link to. If you'd rather link to the day archive list, use get_day_link($year, $month, $day) instead of get_permalink.
                    // This line compares the query year to the post year. If it's the same year, then add "Also from" so that it doesn't look like a duplicate link. 
                    echo '<a class="on-this-day" href="'.get_permalink($query->ID).'" title="On this day in '.$year.' - '.get_the_title().'">Also from '.$year.' - '.get_the_title().'</a><br> ';
                } else {
                    echo '<a class="on-this-day" href="'.get_permalink($query->ID).'" title="On this day in '.$year.' - '.get_the_title().'">'.$year.' - '.get_the_title().'</a><br> ';
                }
            } 
        // Reset post data so it displays the original post. 
        wp_reset_postdata();    
         } else {
         // No posts to show
         echo "No Historical Posts On This Day";
        }
    }
    
    
    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
        // Posts for all years before the current post date.
        //$where .= " AND post_date < '" . get_the_date('Y-m-d') . "'";
        // Filter on post ID instead
        $where .= " AND ID <> '" . get_the_ID() . "'";
        return $where;
    }