'post',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
) );
$current_month = '';
// Loop through all posts and group them by month.
if ( $post_items->have_posts() ) {
while ( $post_items->have_posts() ) {
$post_items->the_post();
$the_month = get_the_date( 'F' );
$the_year = get_the_date( 'Y' );
// If no month is set yet, this is the first iteration; start a new group.
if ( empty( $current_month ) ) {
$current_month = $the_month;
echo '' . esc_html( $the_month . ' ' . $the_year ) . '
';
echo '';
} else {
// If the month changes, close the previous
and start a new one.
if ( $current_month !== $the_month ) {
echo '
' . esc_html( $the_month . ' ' . $the_year ) . '
';
echo '';
$current_month = $the_month;
}
}
// List each post with date and title.
printf(
'