Select Page

WordPress offers easy ways to query posts. There are dozens of conditions you can apply to query posts.

Today, I will show you an easy way to query and display posts published this month.

Here’s the code you can use:

$posts = get_posts(array(
	'numberposts' => 10, // The number of posts to retrieve.
	'monthnum' => date('m'), // This month.
	'year' => date('Y'), // This year.
));

The get_posts function is a quick and easy wrapper for WP_Query. In the arguments for WP_Query, you can specify the month and year for which you want to query posts. In the above example, we are querying posts published this month of this year.

You can then display the resulting posts like this:

$posts = get_posts(array(
	'numberposts' => 10, // The number of posts to retrieve.
	'monthnum' => date('m'), // This month.
	'year' => date('Y'), // This year.
));

if( ! empty( $posts ) ){
	$output = '<ul>';
	foreach ( $posts as $p ){
		$output .= '<li><a href="' . get_permalink( $p->ID ) . '">' . $p->post_title . '</a></li>';
	}
	$output .= '</ul>';
}

echo $output;