Reputation: 10696
How to modify the wp_query to take in count post by month as per URL 2012/10/
?
The following will list ALL post, where I just want to list post for 2012/10/
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 10, 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
Any suggestions much appreciated.
Upvotes: 0
Views: 7371
Reputation: 2284
There's a code snippet provided in the link from the earlier answer that says "Returns posts for just the current week". Change "week" for "month", and you got your answer.
The code snippet would look like this:
$year = date('Y');
$month = date('n');
$query = new WP_Query( 'year=' . $year . '&monthnum=' . $month );
Just read the link. It's all there.
EDIT: If there are further problems returning the month, it may be because the variable needs a leading zero. In that case, the $month variable should be
$month = date('m');
Upvotes: 3
Reputation: 12537
You can do that with the year
and monthnum
parameters, as described in the section Time Parameters in the class reference:
// ... set up your argument array for WP_Query:
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC' ,
// add the year/month query here:
'year' => 2012,
'monthnum' => 10
);
$wp_query = new WP_Query($args);
// ... and so on
Upvotes: 2