Add 20yrs to post date, and then query

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="https://wordpress.stackexchange.com/questions/46324/<?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="https://wordpress.stackexchange.com/questions/46324/<?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>

Leave a Comment