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

I don't quite get why this isn't working. I'm trying to use the following to only show posts from the current year on the front page:

<?php query_posts( "&year=$current_year&order=DESC"); ?>

Yet it still shows posts from 2012 (they weren't actually made in 2012, but I set the publish date to one of the posts that's showing to Feb of last year)

According to the documentation, that's how I should be doing it. Can anyone shed any light?


share|improve this question
Nevermind, I forgot to set the variable: <?php $current_year = date('Y'); ?> /Facepalm – Phill Mar 10 '13 at 3:09

You just need get current date and add it on data_query in wp_query, Look this:


$getdate = getdate();
$args = array(
    'date_query' => array(
            'year'  => $getdate["year"]
$query = new WP_Query( $args );


and then use loop:


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


endwhile; endif;

share|improve this answer

Don't use query_posts to modify your queries. It's a waste of resources and it produces unpredictable results, as it overwrites the original query which happens before the template is loaded.

Use pre_get_posts in your theme's functions.php instead to alter the query before it is sent to the database, and before the template is loaded.

function home_page_current_year( $query ){
    if( $query->is_home() && $query->is_main_query() )
        $query->set( 'year', date('Y') );
add_action( 'pre_get_posts', 'home_page_current_year' );
share|improve this answer

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.