title: Building a PHP/WordPress Calendar
body: <p>I've slightly adapted a PHP based calendar from David Walsh (<a href="https://davidwalsh.name/php-calendar" rel="nofollow noreferrer">https://davidwalsh.name/php-calendar</a>).</p>
  
  <p>For each day, I want to be able to output posts based on the date. This work well with a date query, but isn't so user friendly and future posts, don't look so nice with "Scheduled" marked in the back office nor the /?p=1234 URL.</p>
  
  <p>How would this be adapted to use an ACF date picker field and compared to the current calendar cell for output?</p>
  
  <p>I can grab the meta data easily enough, but is this possible to compare meta values against variables within the loop?</p>
  
  <p>Can I then use another meta field to order?</p>
  
  <pre><code>for($list_day = 1; $list_day &lt;= $days_in_month; $list_day++):
  
     // FYI day/month/year vars avaliable to use to query
     $list_day;
     $month;
     $year;
  
     // Reset entries
     $entries = '';
  
     // Set query arguments
     $args = array(
        'post_type' =&gt; 'post',
        'posts_per_page' =&gt; -1,
        'meta_key' =&gt; 'event_date',
        'meta_query' =&gt; array(
           'key' =&gt; 'event_date',
           'value' =&gt; date( 'dnY' ), 
           'compare' =&gt; '&gt;=',
           'type' =&gt; 'DATE'
        ),
        'orderby' =&gt; array(
           'event_start' =&gt; 'DESC'
        ),
        'post_status' =&gt; array('publish', 'future')
     );
  
     // Run query
     $wp_query = new WP_Query($args);
  
     if($wp_query-&gt;have_posts()) : while($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post();
  
        // Collect entries
        $entries .= '&lt;a class="calendar-entry" href="'.get_permalink().'"&gt;'.get_the_title().'&lt;/a&gt;';
  
     endwhile; endif;
  
     // Apply class dependant on entries
     if($entries) {
        $day_class = 'has-entries';
     } else {
        $day_class = 'no-entries';
     }
  
     // Start calendar cell
     $calendar.= '&lt;div class="calendar-day '.$day_class.'"&gt;&lt;div class="inner"&gt;';
     $calendar.= '&lt;div class="day-number"&gt;'.$list_day.'&lt;/div&gt;';
  
     // If entries, print on calendar cell
     if($entries) {
        $calendar.= '&lt;div class="entries"&gt;'.$entries.'&lt;/div&gt;';
     }
  
     // End calendar cell
     $calendar.= '&lt;/div&gt;&lt;/div&gt;';
  
  endfor;
  </code></pre>
  
  <p>I'm guessing something like this isn't the best approach which is achieved by querying all posts?</p>
  
  <pre><code>if($wp_query-&gt;have_posts()) : while($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post();
  
     // Get event date
     $event_date = get_field('event_date');
  
     $event_dates = explode('/',$event_date);
     $event_day = $event_dates[0];
     $event_month = $event_dates[1];
     $event_year = $event_dates[2];
  
     if($event_day == $list_day &amp;&amp; $event_month == $month &amp;&amp; $event_year == $year) {
  
        $entries .= '&lt;a class="calendar-entry '.$month.'" href="'.get_permalink().'"&gt;'.get_the_title().'&lt;/a&gt;';
  
     }
  
  endwhile; endif;
  </code></pre>