Skip to content

Instantly share code, notes, and snippets.

@Plou
Last active December 1, 2015 11:12
Show Gist options
  • Save Plou/b973618735fe4cbc33cc to your computer and use it in GitHub Desktop.
Save Plou/b973618735fe4cbc33cc to your computer and use it in GitHub Desktop.
Get month and years from list of custom post in wp
<?php
$yearsStart = $wpdb->get_col("SELECT YEAR(FROM_UNIXTIME((meta_value / 1000))) FROM wp_postmeta WHERE meta_key LIKE 'start_date' ORDER BY meta_value ASC");
$yearsEnd = $wpdb->get_col("SELECT YEAR(FROM_UNIXTIME((meta_value / 1000))) FROM wp_postmeta WHERE meta_key LIKE 'end_date' ORDER BY meta_value ASC");
$tempyears = array_unique(array_merge($yearsStart, $yearsEnd));
$currentYear = date('Y');
$currentMonth = date('m');
foreach (array_unique($yearsStart) as $year) {
if($year >= $currentYear) {
$tempMonths = $wpdb->get_col("SELECT DISTINCT MONTH(FROM_UNIXTIME((meta_value / 1000))) FROM wp_postmeta WHERE meta_key LIKE 'start_date' AND YEAR(FROM_UNIXTIME((meta_value / 1000))) = $year ORDER BY meta_value ASC");
foreach ($tempMonths as $month) {
if($year > $currentYear OR $month >= $currentMonth) {
$months[] = $month;
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment