1

So pre_get_posts seems to be the way to do this now - a lot of the background is nicely explained here: When to use WP_query(), query_posts() and pre_get_posts

I can happily modify the main loop to show a chosen month using this function:

function loop_current_month( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'monthnum', 3 );
    return;
 }
}
add_action( 'pre_get_posts', 'loop_current_month' );

However, I would like to modify the loop for the current month, and the current year.

How can $query->set() take two vars? Something like:

$query->set( 'monthnum', 3 & 'year', 2013 );

Any ideas?

Many thanks Dave

1 Answer 1

0

have you tried adding a second $query->set() ?

function loop_current_month( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'monthnum', date('m') );
        $query->set( 'year', date("Y") );
    }
}
add_action( 'pre_get_posts', 'loop_current_month' );

This should get all the posts from the current month or this year, note date('m') and date('Y')

1
  • That'll do it - thanks! Thanks also for the date function tip, I was going to do that outside of the loop function, then add the variables, but putting it straight it makes much more sense :) Cheers.
    – Dave
    Sep 4, 2013 at 10:55

Your Answer

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

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