Add 20yrs to post date, and then query

I would like to know if anyone knows a way of adding a timeframe (say 20 years) to the datestamp on a set of posts, and then running a query on the loop which uses the new date?

Basically what I want to do is have a series of normal news posts. In addition to that, I have a custom post type for ’20 years ago today’, with the posts in that date stamped appropriately in the early 90s. I’d like for those posts to appear in the same loop as the news posts (rather than in a separate loop).

Read More

So it might go:

  • News Post (21 March 2012)
  • News Post (19 March 2012)
  • 20 years ago (17 March 1992)
  • News Post (12 March 2012)
  • 20 years ago (6 March 1992)

and so on. And anything that happened less than 20 years ago (say, in 1993), would behave like a pending post, and not show up.

Any suggestions would be appreciated

Related posts

Leave a Reply

1 comment

  1. An example, that uses the posts_where filter. If you need to extend a query using the posts_clauses filter, then just exchange $where with $pieces and set $pieces['where'] .= instead of $where .=. Just drop that into your functions.php file and add some conditional tag before querying the posts.

    function filter_where( $where ) 
    {
        // Add some conditionals and abort if they're not met:
        // @example Abort on pages
        if ( ! is_page() )
            return;
    
        // posts in the last 30 days
        // $where .= " AND post_date > '".date( 'Y-m-d', strtotime( '-30 days' ) )."'";
    
        // posts  30 to 60 days old
        // $where .= " AND post_date >= '".date( 'Y-m-d', strtotime( '-60 days' ) )."'"." AND post_date <= '".date( 'Y-m-d', strtotime( '-30 days' ) )."'";
    
        // posts between 01.03.2012 and 21.03.2012
        $where .= " AND post_date >= '2012-03-01' AND post_date <= '2012-03-21'";
    
        return $where;
    }
    add_filter( 'posts_where', 'filter_where' );
    

    Edit: So, here’s the original loop from the OP:

    <section id="content">
    <?php 
    query_posts( array( 'post_type' => array( 'post', 'twentyyearsago' ) ) ); 
    if ( have_posts() )
    {
    while ( have_posts() )
    {
        the_post();
    
        if ( 'twentyyearsago' === get_post_type() ) // FIX THIS: strict type checking with "==="
        {
        ?>
            <article class="twentyyears">
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="This day 20 years ago">This day 20 years ago</a></h2>
                <?php the_content(); ?>
                <footer><?php the_time( 'F j, Y' );?></footer> // FIX THIS: One leading "<" in front of "<?php" too much
            </article>
        <?php } else { ?>
            <article class="post">
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                <?php the_content(); ?>
                <footer><?php the_time( 'F j, Y' ); ?></footer>
            </article>
    
        <?php 
        } // FIX THIS: one trailing ";" too much
    } // endwhile;
    else
    {
        // do stuff
        echo "No Posts.";
    } // endif; 
    wp_reset_query(); 
    ?>
    </section>