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?

Thanks

2 Answers
2

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

<?php 

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

?>

and then use loop:

<?php 

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

the_title();
the_content();

endwhile; endif;

?>

Leave a Reply

Your email address will not be published. Required fields are marked *