title: WordPress: Order WP_Query by custom date field and split the loop after every month
body: <p>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:</p>
  
  <pre><code>$current_date_query = date ('Y-m-d');
  
  $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query();
  
  $wp_query-&gt;query(
      array(
         'post_type' =&gt; 'event',
         'meta_key' =&gt; 'event_date',
         'meta_compare' =&gt; '&gt;=',
         'meta_value' =&gt; $current_date_query,
         'post_status' =&gt; 'publish',
         'posts_per_page' =&gt; '99',
         'orderby' =&gt; 'meta_value',
         'order' =&gt; 'ASC',
         'paged' =&gt; $paged
      )
  ); ?&gt;
  
  
  ... Loop stuff ...
  
  &lt;?php endif; $wp_query = null; $wp_query = $temp; wp_reset_query(); ?&gt;
  </code></pre>
  
  <p>I now want to add a headline before every new month. Like this:</p>
  
  <p><strong>January 2018</strong></p>
  
  <ul>
  <li>Event 1</li>
  <li>Event 2</li>
  <li>Event 3</li>
  </ul>
  
  <p><strong>February 2018</strong></p>
  
  <ul>
  <li>Event 4</li>
  <li>Event 5</li>
  </ul>
  
  <p><strong>March 2018</strong></p>
  
  <ul>
  <li>Event 5</li>
  </ul>
  
  <p>and so on...</p>
  
  <p>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.</p>
  
  <p>I tried the following solution (thanks to the comment from @FluffyKitten):</p>
  
  <pre><code>&lt;?php
  
  /* declare an array to save the data */
  $quarters = array();
  
  foreach($posts as $q) {
  
      $donor_date = get_post_meta($q-&gt;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 =&gt; $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){ ?&gt;
  
           &lt;h3&gt;&lt;?php echo $quartername; ?&gt;&lt;/h3&gt;
           &lt;table&gt;
           &lt;?php  foreach ($quarter as $qdate ){ ?&gt;
               &lt;tr&gt;&lt;td&gt; &lt;?php echo $qdate; ?&gt; &lt;/td&gt;&lt;/tr&gt;
           &lt;?php  }  // end foreach($quarter...) ?&gt;
           &lt;/table&gt;
       &lt;?php
       } // end if
  } // end foreach($quarters...)
  ?&gt;
  </code></pre>
  
  <p>Now I get a result like this:</p>
  
  <p><strong>Ocotber</strong></p>
  
  <ul>
  <li>02/10/2018 </li>
  <li>29/10/2017</li>
  </ul>
  
  <p><strong>January</strong></p>
  
  <ul>
  <li>02/01/2018</li>
  </ul>
  
  <p><strong>December</strong></p>
  
  <ul>
  <li>02/12/2017</li>
  <li>01/12/2017</li>
  </ul>
  
  <p><strong>May</strong></p>
  
  <ul>
  <li>02/05/2018</li>
  </ul>
  
  <p>...</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Edit: I could order the month by using a custom query like this:</p>
  
  <pre><code>$posts = query_posts( array(
      'post_type' =&gt; 'event',
      'meta_key' =&gt; 'event_date',
      'meta_compare' =&gt; '&gt;=',
      'meta_value' =&gt; $current_date_query,
      'post_status' =&gt; 'publish',
      'posts_per_page' =&gt; '99',
      'orderby' =&gt; 'meta_value',
      'order' =&gt; 'ASC',
      'paged' =&gt; $paged
  ) );
  </code></pre>
  
  <p>The problem now is only the year. It's all sort by month with different years per month.</p>