1

I would like to loop through all posts within a certain year. Pretty simple setup:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   // doing content stuff here 
<?php endwhile; ?> 
<?php endif; ?>

How can I limit this for all posts for the year of X?

Thanks for the help!

1
  • On which template is this. Got a feeling that you might not need a custom query Commented Jan 1, 2015 at 18:15

1 Answer 1

4

You will have to run a custom query for that. Here is the query for displaying posts from a particular year. I have used year 2012 as an example.

$args = array(
  'post_type' => 'post',
  'ignore_sticky_posts' => 1,
  'year'  => '2012',
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) :
  while ( $the_query->have_posts() ) : $the_query->the_post();

      the_content();

  endwhile;
endif;

wp_reset_postdata();
5
  • thanks, could you please do a simple output of the_content(); in your example. Somehow it does not work for me! Appreciate!
    – user49869
    Commented Jan 1, 2015 at 18:08
  • I have updated the code and added the_content(). Please try again.
    – Robert hue
    Commented Jan 1, 2015 at 18:09
  • sorry to bother you again, but I noticed a very weird behaviour on this snippet. When I use the_date(); it only gives me a date on the LATEST post within the loop. Do you know why?
    – user49869
    Commented Jan 1, 2015 at 20:00
  • Use <?php echo get_the_date(); ?> instead of the_date(). Why it does not work, you can read more about it here.
    – Robert hue
    Commented Jan 1, 2015 at 20:06
  • @user49869 I think Robert has solved your problem and provided you with a link that explains the the_date() issue, so please accept the answer to close the question, thanks.
    – birgire
    Commented Jan 27, 2015 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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