Question

Edit #3 - meta_keys as an array

This seems to compare more than just the "opening_time" I can set the opening time for the wrong date or even leave it blank and it will still grab a post if the "panel_time" is in range.

Am I missing something?

$keys = array('opening_time', 'closing_time', 'artist_talk_time', 'special_event_time', 'lecture_time', 'panel_time', 'workshop_time');

$args = array(
 'post_type' => 'event',
 'orderby'     => 'meta_value',
 'order'       => 'asc',
 'meta_query' => array(
 'relation' => 'OR',
    array(
        'key' => $keys,
        'value' => array($today,$future),
        'compare' => 'BETWEEN',
        'type' => 'DATE'
    ),

)
);

Edit #2 to show sorting of met_keys after the posts query

This is the code that sorts all the posts by their respective meta keys. It moves this into days and orders by start date whether it is a panel or a workshop.
I just need a way to get posts to this stage.

It would be stupid to runt all "events" custom posts types through this since after a few years the list would be very long for the computer to chunk through right?

Any suggestions?

      <?php 
  $event_query = new WP_Query( $args ); 
  if ($event_query->have_posts()) : while ($event_query->have_posts()) :  $event_query->the_post();  

  $keys = array('opening_time', 'closing_time', 'artist_talk_time', 'special_event_time', 'lecture_time', 'panel_time', 'workshop_time');
  $custom_field_keys = get_post_custom_keys();

  foreach ($custom_field_keys as $custom_field_key) {
     if (in_array($custom_field_key, $keys)) {    
          $custom_field_value = get_post_meta($post->ID, $custom_field_key, true);
          if ($custom_field_value >= $thedate && $custom_field_value <= $future) {
             $counttest++;
             $times[] = array($custom_field_value, $post->ID, $custom_field_key);
          }
      }
  }     
  endwhile; 

  if ($counttest >0) {

  echo $counttest;

  if ($counttest>=2) {
          echo ' Events ';
      } 
      if ($counttest<2) { 
          echo ' Event ';
      }
  }
  endif;
  $counttest=0;

  asort($times); 

  foreach ($times as $event) { $time_value = $event[0]; $post_id = $event[1]; $time_key = $event[2];  // changed the foreach here

      if ($time_key == 'opening_time') {/* do something */  }

      else if ($time_key == 'artist_talk_time') { /* do something */ }
      else if ($time_key == 'closing_time') { /* do something */ }
      else if ($time_key == 'special_event_time') { /* do something */ }

      rewind_posts();
   }
   ?>

Edit to add suggestion about another possible solution to simplify things

Can I put the keys as a series like the code below? I put it into my test site and it seems to work because I have a function below that sorts the posts by the key values. Or does this have problems of its own?

$args = array(
 'post_type' => 'event',
 'orderby'     => 'meta_value',
 'order'       => 'asc',
 'meta_query' => array(
 'relation' => 'OR',
    array(
        'key' => 'opening_time','closing_time','artist_talk_time','special_event_time',
        'value' => array($today,$future),
        'compare' => 'BETWEEN',
        'type' => 'DATE'
    ),

)
);

Have been trying to figure out where the bottle neck in my code is. I commented different parts to try to isolate to offending portion. If I comment out this section of code it runs at normal speed, but with this query it can take an very long time to load + 10 seconds.

Is there an inherent inefficiancy with this query? Is there an easier way to do it. All of the key values are in the format Y-m-d 00:00.

Thank you in advance.

<?php   
  $today = date("Y-m-d 00:00");  //set today's date for upcomming events sidebar
  for ($i=0; $i<10; $i++) {
  $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
  $thedate = date ( 'Y-m-d H:i' , $thedate );

  $thedaytext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
  $thedaytext = date ( 'l' , $thedaytext );

  $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaydatetext ) ) ;
  $thedatetext = date ( 'F j' , $thedatetext );

  $future = strtotime ( '+24 hours' , strtotime ( $thedate ) ) ;
  $future = date ( 'Y-m-d H:i' , $future ); 
  $times = array();  

  $args = array(
       'post_type' => 'event',
       'orderby'     => 'meta_value',
       'order'       => 'asc',
       'meta_query' => array(
       'relation' => 'OR',
          array(
              'key' => 'opening_time',
              'value' => array($today,$future),
              'compare' => 'BETWEEN',
              'type' => 'DATE'
          ),
          array(
              'key' => 'artist_talk_time',
              'value' => array($today,$future),
              'compare' => 'BETWEEN',
              'type' => 'DATE'
          ),
          array(
              'key' => 'closing_time',
              'value' => array($today,$future),
              'compare' => 'BETWEEN',
              'type' => 'DATE'
          ),
          array(
              'key' => 'special_event_time',
              'value' => array($today,$future),
              'compare' => 'BETWEEN',
              'type' => 'DATE'  
          )
      )
  );

  $event_query = new WP_Query( $args ); 

  if ($event_query->have_posts()) : while ($event_query->have_posts()) :  $event_query->the_post();

  endwhile;
  endif;
  }

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top