Created
May 14, 2011 17:46
-
-
Save mrlannigan/972433 to your computer and use it in GitHub Desktop.
WPquestions.com #2245 rev1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // answer to wpquestions.com #2245 by Julian Lannigan (julianlannigan.com) | |
// GET THE DATA | |
$customFieldName = 'event_date'; | |
$outputData = array(); | |
$currentDate = time(); | |
$queryArgs = array( | |
'posts_per_page' => -1, | |
'meta_query' => array( | |
array( | |
'key' => $customFieldName, | |
'value' => "", | |
'compare' => "!=", | |
'type' => 'CHAR' | |
) | |
) | |
); | |
$eventQuery = new WP_Query($queryArgs); | |
if ($eventQuery->have_posts()) { | |
foreach ($eventQuery->posts as $ePost) { | |
$pID = $ePost->ID; | |
$cfEventDate = get_post_meta($pID, $customFieldName, 1); | |
//First we will try to convert the date using PHP. | |
//$cfEventDateConvert = strtotime($cfEventDate); | |
//if ($cfEventDateConvert == false) { | |
//With the given format(assuming dd/mm/yyyy), we have to flip the date into a valid format specified here: | |
// http://www.php.net/manual/en/datetime.formats.date.php | |
//The following changes the format from dd/mm/yyyy to mm/dd/yyyy | |
$cfEventDate = explode("/", $cfEventDate); | |
if (count($cfEventDate) != 3) { | |
continue; | |
} | |
$EventDateDAY = $cfEventDate[0]; | |
$EventDateMONTH = $cfEventDate[1]; | |
$cfEventDate[0] = $EventDateMONTH; | |
$cfEventDate[1] = $EventDateDAY; | |
$cfEventDate = strtotime(implode("/", $cfEventDate)); | |
if ($cfEventDate == false) { | |
continue; | |
} | |
//DONE with date conversion | |
//} else { | |
//Everything is good, you are using a supported PHP date format. | |
// $cfEventDate = $cfEventDateConvert; | |
//} | |
if ($cfEventDate < $currentDate) { | |
continue; //don't display old events | |
} | |
$keyDate = date('Ym', $cfEventDate); | |
if (!isset($outputData[$keyDate])) { | |
$outputData[$keyDate] = array(); | |
} | |
$makeUnique = ""; | |
if (isset($outputData[$keyDate][date('d',$cfEventDate)])) { | |
$makeUnique = ".".mt_rand(); | |
} | |
$outputData[$keyDate][date('d',$cfEventDate).$makeUnique] = array('eventDate'=>$cfEventDate, 'post'=>$ePost); | |
ksort($outputData[$keyDate], SORT_NUMERIC); | |
} | |
ksort($outputData, SORT_NUMERIC); | |
} | |
//END DATA GET | |
//START DISPLAY OF DATA | |
foreach ($outputData as $yearMonth => $data) { | |
$oYear = substr($yearMonth, 0, 4); | |
$oMonth = substr($yearMonth, -2); | |
$oYearMonthTimestamp = mktime(0,0,0,$oMonth,1,$oYear); | |
//Display header for year/month | |
echo "<h2>".strtoupper(date("M / Y", $oYearMonthTimestamp))."</h2>"; | |
//Display list of events in that year/month | |
echo "<ul>"; | |
foreach ($data as $day => $postInfo) { | |
$oDay = substr($day, 0, 2); | |
echo "<li><a href=\"".get_permalink($postInfo['post']->ID)."\">"; | |
echo "{$postInfo['post']->post_title}"; | |
echo "</a></li>"; | |
} | |
echo "</ul>"; | |
echo "<hr>"; | |
} | |
//END DISPLAY OF DATA | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment