Ok, I have a client that wants to only display posts by the current month. I’ve tried a few different variations but none have worked. Here’s where I’m at now. Any help is appreciated!
<?php
$current_year = date('Y');
$current_month = date('m');
$args = array(
'year' => $current_year,
'monthnum' => $current_month
);
$custom_query = new WP_Query('$args');
if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();
// The following determines what the post format is and shows the correct file accordingly
$format = get_post_format();
get_template_part( 'includes/post-formats/'.$format );
if($format == '')
get_template_part( 'includes/post-formats/standard' );
endwhile; else:
?>
$current_month = date('m');
should be$current_month = date('n');
Reference the page for the WP_Query object:
codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters
“month_num” is an int from “1-12”, not from “01-12”. The way the variables are currently set up, $current_month has a preceding zero. I’m assuming that a simple change to return the month without a preceding zero will fix this. I’m assuming that the query is erroring out because of this improperly formatted argument. Also, make sure as stated in the comments above that you don’t put quotes around “$args” in WP_Query.