WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trying to get posts from a specific category for specific years. Nothing is working, including this:

<?php $args=array(
'cat' => '11', //Category I'm targeting
'year' => 2008,2009,2010 //Years I want
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

That code will only display posts for 2008.

share|improve this question

You should use a date range instead by filtering posts_where:

function wpse_43397_where($where=''){
    $where .= " AND post_date >= '2008-01-01' AND post_date < '2010-12-31'";
    return $where;

add_filter('posts_where', 'wpse_43397_where');
$my_query = new WP_Query($args);
remove_filter('posts_where', 'wpse_43397_where');
share|improve this answer
+1 exact same answer, just 34 seconds quicker :D – Stephen Harris Feb 24 '12 at 17:22
@StephenHarris haha I noticed that. +1 to you as well. :) – Brian Fegter Feb 24 '12 at 17:23
But you both forgot that this question was about getting posts from a specific category. ;) – Nate Feb 24 '12 at 17:28
Not true, use the query you had created already. You can still pass $args to your WP_Query object – Brian Fegter Feb 24 '12 at 17:33
Oh, ok great! Thank you for the help! – Nate Feb 24 '12 at 17:57

This isn't possible with the WP_Query API:

Note: The queries above return posts for a specific date period in history, i.e. "Posts from X year, X month, X day". They are unable to fetch posts from a timespan relative to the present, so queries like "Posts from the last 30 days" or "Posts from the last year" are not possible with a basic query, and require use of the posts_where filter to be completed. The examples below use the posts_where filter, and should be modifyable for most time-relative queries.

So you will need to add a custom filter before your query, perform the query and then remove the query again (so it doesn't effect other queries).

// Create a new filtering function that will add our where clause to the query
function my_restrict_to_years( $where = '' ) {
    // posts  in years 2008-2010 
    $where .= " AND post_date >= '2008-01-01' AND post_date <= '2010-12-31'";
    return $where;

add_filter( 'posts_where', 'my_restrict_to_years' );
 'cat' => '11', //Category I'm targeting
$my_query = new WP_Query($args);
remove_filter( 'posts_where', 'my_restrict_to_years' );

Not tested

share|improve this answer
What about the category? How do I add that as part of the filter? – Nate Feb 24 '12 at 17:27
Just replace $query_string with your $args, leaving out the year part. – Stephen Harris Feb 24 '12 at 17:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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