2

I have found a way to show all posts from the current year with this code:

<?php
$today = getdate();
$lastyear = $today[ 'year' ];
$args = array( 
    //'nopaging' => true,
    'paged' => $paged,
    'category' => 1, 
    'posts_per_page' => 36,
    'year' => $lastyear,
);
query_posts( $args ); // The Query ?>

    <div id="grid">

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

    </div>

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

<?php wp_reset_query(); // Reset Query ?>

However, what I really want is to show posts from 2 years ago till today (it's Jan 2014), meaning, posts from 2012 and 2013 and 2014. How do I do this?

Many thanks in advance.

2
<?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

  • This works beautifully, thus you, my friend, are a genius. I did see that article, but for a coding-newbie like myself, I couldn't write the proper code to get it to retrieve past 2 years from the 1st onwards, as I've seen you do so in your code. This sparks another question though, does this mean from February onwards I can only see posts from February 2012 and not January 2012? Thank you so much again. – Natoli Jan 22 '14 at 8:20
  • @s_ha_dum You're right, changed it (didn't see the query_posts()) Natoli Yes that means you see posts fromt feb and onwards, if you don't want that, just change the var to hardcoded January. – Rob Vermeer Jan 27 '14 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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