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.

So I've got a wordpress blog I've been posting to once a week for a couple of years - I'd like to be able to press a button, or use a bookmarklet, or edit the url in such a way that I see that I see the post from a year ago.

For example if the date is 31 December 2012. I'd like to be able to retrive the post that was made closest to 31 December 2011. Any ideas?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Here is some code I drafted up. It may not be using the best practices or even work for the matter of fact, as I have not tested it, but theoretically it should work. :)

<?php
/**
 * Retrieve the closest post from a set time from today.
 * 
 * @param  integer $daysago the number of days 
 * @return integer the ID of the post from long ago
 **/

function wpse38126_filterfoward($where = '', $daysago) { $where .= " AND post_date > '" . date('Y-m-d', strtotime('-' . $daysago . ' days')) . "'"; return $where; }
function wpse38126_filterbackward($where = '', $daysago) { $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-' . $daysago*2 . ' days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-'.$daysago.' days')) . "'"; }
function wpse38126_limits($limits) { return 1; }
function wpse38126_postfromlongago( $daysago = '') {
    $month = date('n', strtotime('-' . $daysago . ' days'));
    $day   = date('j', strtotime('-' . $daysago . ' days'));
    $year  = date('Y', strtotime('-' . $daysago . ' days'));
    $query = new WP_Query(array(
        'month' => $month,
        'day' => $day,
        'year' => $year ));
    if($query->have_posts()) { while($query->have_posts()) { $query->the_post();
        return $post->ID;
    } } else {
        add_filter( 'posts_where', 'wpse38126_filterfoward' );
        add_filter( 'post_limits', 'wpse38126_limits' );
        $fowardintime = new WP_Query( $query_string . '&order=ASC&orderby=date' );
        remove_filter( 'posts_where', 'filtwpse38126_filterfowarder' );
        remove_filter( 'post_limits', 'wpse38126_limits' );

        add_filter( 'posts_where', 'wpse38126_filterbackward' );
        add_filter( 'post_limits', 'wpse38126_limits' );
        $backwardintime = new WP_Query( $query_string );
        remove_filter( 'posts_where', 'wpse38126_filterbackward' );
        remove_filter( 'post_limits', 'wpse38126_limits' );

        // set up posts
        if($fowardintime->have_posts) { while($fowardintime->have_posts()) { $fowardintime->the_post();
            $fowardintime_id = $post->ID; $fowardintime_date = get_the_date( 'Y/m/d g:i:s A' );
        }
        if($backwardintime->have_posts) { while($backwardintime->have_posts()) { $backwardintime->the_post();
            $backwardintime_id = $post->ID; $backwardintime_date = get_the_date( 'Y/m/d g:i:s A' );
        }

        // do some complicated logical comparisons
        if($fowardintime_id && !$backwardintime_id) {
            return $fowardintime_id;
        } elseif ($backwardintime_id && !$fowardintime_id) {
            return $backwardintime_id;
        } else {
            $foward = strtotime($fowardintime_date) - strtotime('-' . $daysago . ' days');
            $backward = strtotime('-' . $daysago . ' days') - strtotime($backwardintime_date);

            if ($foward < $backward) {
                return $fowardintime_id;
            } else {
                return $backwardintime_id;
            }
        }
    }
}
?>

Oh, and here is the usage. Say you wanted to post a link to the post from a year ago.

<?php $post_id = wpse38126_postfromlongago(365); ?>
<a href="<?php echo get_permalink( $post_id ); ?>">My link to a post or page</a>

Anyone is free to help improve it.

share|improve this answer
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.