Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Created October 21, 2021 19:11
Show Gist options
  • Save everaldomatias/f7f03e0a257fb4d72d90db9208c4b215 to your computer and use it in GitHub Desktop.
Save everaldomatias/f7f03e0a257fb4d72d90db9208c4b215 to your computer and use it in GitHub Desktop.
Get and separate posts by months
<?php
/**
* @author https://wordpress.stackexchange.com/users/10097/david-gard
* @link https://wordpress.stackexchange.com/questions/176141/display-posts-by-month
*/
$time_start = microtime(true);
/** Set up a date interval object for 6 monts ago (you can change as required) */
$interval = new DateInterval('P6M');
$interval->invert = 1;
/** Grab the date as it was 6 months ago */
$date = new DateTime(date('Y-m-d'));
$date->add($interval);
/** Query the database for all posts newer than the the given date interval */
$args = array(
'nopaging' => true,
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'date_query' => array(
'after' => $date->format('Y-m-d')
)
);
$month_query = new WP_Query($args);
/** Check to ensure that there are articles for this month... */
if($month_query->have_posts()) :
$month_titles = array();
$close_ul = false;
/** Set the attributes for displaying the title as an attribute */
$title_attribute_args = array(
'before' => 'Visit article \'',
'after' => '\' ',
'echo' => false
);
/** Loop through each post for this month... */
while($month_query->have_posts()) : $month_query->the_post();
/** Check the month/year of the current post */
$month_title = date('F Y', strtotime(get_the_date('Y-m-d H:i:s')));
/** Maybe output a human friendly date, if it's not already been output */
if(!in_array($month_title, $month_titles)) :
if($close_ul) echo '</ul>'; // Check if the unordered list of posts should be closed (it shouldn't for the first '$monthe_title')
echo '<h1 style="padding-left: 250px;" id="monthly-title">' . $month_title . '</h1>'; // Output the '$month_title'
echo '<ul style="padding-left: 250px;" id="monthly-posts">'; // Open an unordered lists for the posts that are to come
$month_titles[] = $month_title; // Add this `$month_title' to the array of `$month_titles` so that it is not repeated
$close_ul = true; // Indicate that the unordered list should be closed at the next oppurtunity
endif;
/** Output each article for this month */
printf(
'<li id="monthly-post-%1$s">%2$s <a href="%3$s" title="%4$s">%3$s</a></li>',
get_the_ID(), /** %1$s - The ID of the post */
get_the_title(), /** %2$s - The article title */
get_permalink(get_the_ID()), /** %3$s - The article link */
the_title_attribute($title_attribute_args) /** %4$s - The title for use as an attribute */
);
endwhile;
if($close_ul) echo '</ul>'; // Close the last unordered list of posts (if there are any shown)
endif;
/** Reset the query so that WP doesn't do funky stuff */
wp_reset_query();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment