I’d recommend something like the following:
-
Slurp up a list of all of the distinct meta values corresponding to the start date meta_key. I don’t see a WP function that does this, so you’ll have to do something like the following:
$start_dates = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", 'start_date' ) );
-
Loop through the start dates to sniff out the years and months that are used, and put them into a structured array:
$year_months = array(); foreach( (array) $start_dates as $sd ) { // Assuming your $sd is already in UNIX format. strtotime() if not $year = date( 'Y', $sd ); $month = date( 'm', $sd ); if ( !isset( $year_months[$year] ) ) { $year_months[$year] = array(); } else { if ( !in_array( $month, $year_months[$year] ) ) { $year_months[$year][] = $month; } } } // sort arsort( $year_months );
That’ll give you an array that looks like
[2012] => 'January', 'March', 'June', [2011] => 'April', 'March', etc
(though you may need to do some usort() magic on these month arrays to get them in the right order). Since this is a costly-ish process, you might want to stash it in an option or a transient, which gets busted on
update_post_meta()
. -
Loop through the array to build your list:
$html="<ul>"; foreach( (array) $year_months as $year => $months ) { $html .= '<li><h3>' . esc_html( $year ) . '</h3>'; $html .= '<ul>'; foreach( $months as $month ) { $html .= '<li><a href="' . $whatever_your_link_is . '">' . esc_html( $month ) . '</a></li>'; } $html .= '</ul>'; $html .= '</li>'; } $html="</ul>"; echo $html;
-
As for how you get
$whatever_your_link_is
, probably the simplest way is to pass meta_value and meta_key arguments to the URL and let WP_Query take care of the rest, though if you need something more sophisticated, you’ll probably have to pass some custom URL parameters and then usemeta_query
.
Good luck!