WordPress Custom Query to show posts from last x years

<?php
$today = getdate();
$args = array( 
    //'nopaging' => true,
    'paged' => $paged,
    'category' => 1, 
    'posts_per_page' => 36,
    'date_query' => array(
        array(
            'after' => $today[ 'month' ] . ' 1st, ' . ($today[ 'year' ] - 2)
        )
    )
);
$catgory_posts = new WP_Query( $args ); // The Query ?>

<div id="grid">

<?php if( $catgory_posts->have_posts() ) while ( $catgory_posts->have_posts() ) : $catgory_posts->the_post(); // The Loop ?>
    <?php get_template_part( 'article', 'thumb' ); ?>
<?php endwhile; ?>

</div>

<?php get_template_part( 'navigation' ); ?>

See date parameters at WordPress codex: http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

Leave a Comment