Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>';
}
share|improve this question
    
Close-voted as off-topic. This is a PHP question, not a WordPress question. –  Chip Bennett May 29 '12 at 14:55
add comment

2 Answers

up vote 3 down vote accepted

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;
}
share|improve this answer
    
Pat--It's kind of poor form to let someone else answer your question (with a complete code example and everything), and then give yourself credit. You should mark @moraleida's answer as the accepted one, as the code is almost entirely what he provided you. –  MathSmath May 30 '12 at 17:57
4  
@MathSmath, thanks for that. I debated what the right thing to do was. I decided that since moraleida's code was "untested" and did not work without modification, I thought it would better the community to paste my modified version of his code. He provided a great foundation on which I built, however given it did not work out of the box, I selected my answer as the correct one. I did provide credit in my very first sentence so commentors know where the foundation was provided from. –  Pat May 30 '12 at 19:05
    
Pat-- Fair enough, I suppose. Though it looks to me like @moraleida did answer your actual question satisfactorily. The only significant adjustment that you made (as far as I can tell) is that your code considers the dates relative to the current posts' publish date rather than the actual current date. This wasn't a requirement defined in your question--if you didn't ask it, how can anybody successfully answer it? And in general, you should expect that all code posted on this site will be untested. If being "untested" disqualified an answer, there would be very few accepted answers. –  MathSmath May 30 '12 at 20:51
add comment

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;

share|improve this answer
    
Okay, newbie moment, How would I correctly display a link for the year if it found one? I know it should be going in the if ($query) section, but I'm not sure how I would display it elegantly. –  Pat May 29 '12 at 15:58
    
Added the loop to it. This should help you get going. –  moraleida May 29 '12 at 16:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.