#StackBounty: #php #wordpress #wordpress-theming WordPress: Order WP_Query by custom date field and split the loop after every month

Bounty: 50

I’m using a WordPress Custom Post Type to manage and display events on my site.
To sort the events I’m using a custom meta field with the date in it (stored like this: YYYY-MM-DD). I check the meta field against the current date. Like this:

$current_date_query = date ('Y-m-d');

$temp = $wp_query; $wp_query= null; $wp_query = new WP_Query();

       '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(); ?>

I now want to add a headline before every new month. Like this:

January 2018

  • Event 1
  • Event 2
  • Event 3

February 2018

  • Event 4
  • Event 5

March 2018

  • Event 5

and so on…

Is there any way to split the loop after every month?
I guess I have to use the month from the meta field but I don’t know how.

I tried the following solution (thanks to the comment from @FluffyKitten):


/* declare an array to save the data */
$quarters = array();

foreach($posts as $q) {

    $donor_date = get_post_meta($q->ID,'gid_22',true);
    $donor_month = date('m',strtotime($donor_date));

    /* format the date once - best practice is not to repeat code */
    $formatteddate = date('d/m/Y',strtotime($donor_date));

    /* organise the dates by quarter, using the heading as the key for easy display */
    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;

 /* now go through your array and display the results */
 foreach ($quarters as $quartername => $quarter){
     /* $quarters won't have any entry for quarters with no dates, so technically this 'if' isn't needed,
       however I've added it in case it will be needed it for any other changes you make */
     if ($quarter){ ?>

         <h3><?php echo $quartername; ?></h3>
         <?php  foreach ($quarter as $qdate ){ ?>
             <tr><td> <?php echo $qdate; ?> </td></tr>
         <?php  }  // end foreach($quarter...) ?>
     } // end if
} // end foreach($quarters...)

Now I get a result like this:


  • 02/10/2018
  • 29/10/2017


  • 02/01/2018


  • 02/12/2017
  • 01/12/2017


  • 02/05/2018

The order is based on the post date and not based on the custom field.
Also there is no difference between years. The year 2017 should have it’s own loop as well as 2018.

If I try to put my own array in the first array of the loop, it has no effect and it only shows as text in the frontend.

Edit: I could order the month by 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 now is only the year. It’s all sort by month with different years per month.

Get this bounty!!!