0
votes

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!

1
WP_Query documentation says that "the ‘type’ DATE works with the ‘compare’ value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format." but $date always return the day of the week name (Monday, Tuesday, etc). Maybe you should change to $date = date('Y-m-d').Cadu De Castro Alves
Right. The day of the week name comes back in var_dump as 'Tuesday' (today) and it also shows that the array is showing too....so the current show on the air on Tuesday should return but it doesn't. I will try changing the format though.EdgarAlexPoe
Here is the var_dump: AND ( mt2.meta_key = 'show_days' AND mt2.meta_value IN ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday') ) AND ( mt3.meta_key = 'date' AND CAST(mt3.meta_value AS DATE) = '2020-04-14' ) )EdgarAlexPoe
meta_value returns 2020-04-14 which confirms the format Y-m-d described in the documentation. Have you tried to change the first line to $date = date('Y-m-d')?Cadu De Castro Alves
Yes thats what I did. However, the show_days will only work based on days of the week, not actual dates. (Mon-Fri, Weekends)....example, one show only airs every Thursday. So I must have the actual days instead of Y-m-dEdgarAlexPoe

1 Answers

0
votes

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