Search code examples
phpwordpressadvanced-custom-fieldsmeta-query

Meta Query Select ACF Not Returning Post For Current Date


I am trying to get a post type to return based on a ACF Select (multiple) of days of the week, Mon-Sund. I set up a meta query with a relation of both the ACF Select key (show_days) and the current date. The var_dump is showing that the two arrays are comparing to each other, but still not showing the show (post type).

<?php

      $date = date('l');
      $shows = get_field('station_shows', false, false);
      $query = new WP_Query ( array(
        'post_type'       => 'shows',
        'posts_per_page'  => 1,
        'post__in'        => $shows,
        'post_status'     => 'publish',
        'meta_query' => array(
            'relation' => 'AND',

           array (
                       'key'           => 'show_days',
                       'value'     => array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
                       'compare'  => 'IN',
                     ),
           array (
                       'key'       => 'date',
                       'value'     => $date,
                       'compare'   => '=',
                       'type'      => 'DATE',
                     ), 

            )));
       if ( $query->have_posts() ) { while( $query->have_posts() ) {
       $query->the_post();
       echo '<div class="onAir"><h3>Currently On Air: ';
       the_title();
       if (get_field('dj', $query->ID)) {
                 $dj = get_field('dj');
                 echo ' w/ ';
              echo $dj;
          }
           echo '</h3></div>';

          } wp_reset_postdata();
        }

 ?>

I don't know if the database meta value is not corresponding. I've even tried to unserialize() the array, but it still returns the same array value. Any help would be appreciated. Thank you!


Solution

  • Okay I figured it out. This query was in conjunction with a time query, so Im putting the entire code up. It is comparing the current time to two custom fields (time ranges) and which days are check marked (checkboxes). So if it 4pm on Tuesday, it will only show that has the time range of 4pm and if Tuesday is check marked. Also, this has a relationship field, where I can assign the shows to certain radio stations. So on the Radio Station post type page, the on air function will show depending on the time of day!

     <?php
          $time = current_time('H:i:s');
          $date = strftime("%u", time());
          $shows = get_field('station_shows', false, false);
          $query = new WP_Query ( array(
            'post_type'       => 'shows',
            'posts_per_page'  => 1,
            'post__in'        => $shows,
            'post_status'     => 'publish',
            'orderby'         => 'meta_value',
            'meta_key'        => 'show_days',
            'meta_query' => array(
                'relation' => 'AND',
            array(
                      'key'       => 'start_time',
                      'value'     => $time,
                      'compare'   => '<=',
                      'type'      => 'TIME',
                ),
            array(
                      'value'     => $time,
                      'key'       => 'end_time',
                      'compare'   => '>=',
                      'type'      => 'TIME',
                ),
    
    
           ))
    
            );
            $days = the_field('show_days');
          if ($query->have_posts()  && $days = $date  )  { while( $query->have_posts() ) {
            $query->the_post();
    
            echo '<div class="onAir"><h3>Currently <span>On Air</span> : ';
            the_title();
               echo '</h3></div>';
    
       }
       wp_reset_postdata();
      }
     ?>