Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to organize and output events by date in a loop
// Example using a while loop (inside WordPress in this case)
$already_echoed = array()
while ( have_posts() ) : the_post();
if (!in_array($date, $already_echoed)) { //check if current date is in the already_echoed array
echo $date;
$already_echoed[] = $date; //store all dates in an array to check against.
// Example using a foreach loop
$already_echoed = array();
foreach ($events as $event) {
if (!in_array($event->date, $already_echoed)) {
echo $event->date . "<br />";
$already_echoed[] = $event->date;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.