I use the WordPress custom post type to manage and display events on my site. To sort events, I use a custom meta field with a date in it (stored as follows: YYYY-MM-DD). I check the meta box for the current date. Like this:
$current_date_query = date ('Ym-d'); $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query( array( 'post_type' => 'event', 'meta_key' => 'event_date', 'meta_compare' => '>=', 'meta_value' => $current_date_query, 'post_status' => 'publish', 'posts_per_page' => '99', 'orderby' => 'meta_value', 'order' => 'ASC', 'paged' => $paged ) ); ?> ... Loop stuff ... <?php endif; $wp_query = null; $wp_query = $temp; wp_reset_query(); ?>
Now I want to add a heading before each new month. Like this:
January 2018
February 2018
March 2018
etc.
Is there a way to split the cycle after every month? I think I need to use the month from the meta field, but I donβt know how to do it.
I tried the following solution (thanks to the comment from @FluffyKitten):
<?php $quarters = array(); foreach($posts as $q) { $donor_date = get_post_meta($q->ID,'gid_22',true); $donor_month = date('m',strtotime($donor_date)); $formatteddate = date('d/m/Y',strtotime($donor_date)); if(in_array($donor_month, array('01'))) $quarters["January"][] = $formatteddate; else if(in_array($donor_month, array('02'))) $quarters["February"][] = $formatteddate; else if(in_array($donor_month, array('03'))) $quarters["March"][] = $formatteddate; else if(in_array($donor_month, array('04'))) $quarters["April"][] = $formatteddate; else if(in_array($donor_month, array('05'))) $quarters["May"][] = $formatteddate; else if(in_array($donor_month, array('06'))) $quarters["June"][] = $formatteddate; else if(in_array($donor_month, array('07'))) $quarters["July"][] = $formatteddate; else if(in_array($donor_month, array('08'))) $quarters["August"][] = $formatteddate; else if(in_array($donor_month, array('09'))) $quarters["September"][] = $formatteddate; else if(in_array($donor_month, array('10'))) $quarters["Ocotber"][] = $formatteddate; else if(in_array($donor_month, array('11'))) $quarters["November"][] = $formatteddate; else if(in_array($donor_month, array('12'))) $quarters["December"][] = $formatteddate; } foreach ($quarters as $quartername => $quarter){ if ($quarter){ ?> <h3><?php echo $quartername; ?></h3> <table> <?php foreach ($quarter as $qdate ){ ?> <tr><td> <?php echo $qdate; ?> </td></tr> <?php } </table> <?php }
Now I get this result:
Ocotber
January
December
May
...
The order is based on the publication date and is not based on a custom field. There is also no difference between the years. 2017 should have its own cycle, as well as 2018.
If I try to put my own array in the first array of the loop, it will have no effect, and it will only display as text in the interface.
Edit: I could order a month using a custom query like this:
$posts = query_posts( array( 'post_type' => 'event', 'meta_key' => 'event_date', 'meta_compare' => '>=', 'meta_value' => $current_date_query, 'post_status' => 'publish', 'posts_per_page' => '99', 'orderby' => 'meta_value', 'order' => 'ASC', 'paged' => $paged ) );
The problem is now only a year. This is all for months with different years per month.