1

EDIT 12/19/2011

Thank you again Ijaas. Everything is working except for the order of the days. It is ordering the groups of days by the publish order. If an older post has a newer date in its meta_key then it is before a newer published post with an older meta_key. It sorts the events on one day properly but it does not sort the days.

Do you have any idea why?

Here is the current code that I am using.

    <h1>Upcoming</h1>
<?php

$convertedtime = "Y-m-d H:i"; // Time format You can remove this if it is defined before
$convertedtime = "g:i"; // convert to 12 hour clock and minutes for upcomming events sidebar
$convertedendtime = "g:i a"; // convert to 12 hour clock and minutes for upcomming events sidebar
$today  = date ( 'Y-m-d H:i' ); 
$thedate = time(); // IF you want to start from a future date use strtotime( FutureDateHere );
$thedate = date ( 'Y-m-d H:i' , $thedate );
$future = strtotime ( '+10 Days' ); // IF you want to start from a future date use strtotime( '+10 Days', strtotime( FutureDateHere ) );
$future = date ( 'Y-m-d H:i' , $future );

$times  = array();
$events = array();

$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' => 'NONE',*/
    array(
        'key' => $keys,
        '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(); 

    // Storing events in array is more efficent than using get_the_title, ect... later on
    $events[$post->ID] = array(
        'title'     => apply_filters('the_title', $post->post_title),
        'link'      => get_permalink($post->ID),
        'thumbnail' => get_the_post_thumbnail($post->ID, 'upcoming_event_sidebar'),
        'venue'     => get_post_meta($post->ID,'event_venue', true),
        'custom_venue' => get_post_meta($post->ID,'custom_event_venue', true),
        'opening_time_end' => get_post_meta($post->ID,'opening_time_end', true),
        'closing_time_end' => get_post_meta($post->ID,'closing_time_end', true),
        'artist_talk_time_end' => get_post_meta($post->ID,'artist_talk_time_end', true),
        'special_event_time_end' => get_post_meta($post->ID,'special_event_time_end', true),
        'lecture_time_end' => get_post_meta($post->ID,'lecture_time_end', true),
        'panel_time_end' => get_post_meta($post->ID,'panel_time_end', true),
        'workshop_time_end' => get_post_meta($post->ID,'workshop_time_end', true)
    );


    $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) {
                $times[date('Y-m-d', strtotime($custom_field_value))][] = array($custom_field_value, $post->ID, $custom_field_key);  
                $events[$post->ID][$custom_field_key] = $custom_field_value; //opening_time, closing_time.......

            }
        }
    }

endwhile;

    foreach($times as $day => $list): if($num = count($list)):

        sort($list);

        echo "<ul>"; // Start a day
        ?>
            <li class="sidebar_event_top">
                <h1>
                    <span class="total">
                        <?php echo $num.(($num >= 2)? " Events " : " Event "); ?>                    
                    </span> <!-- end .total -->
                    <span class="day_sidebar">
                        <?php echo ($day == $today)? "Today" : date( 'l', strtotime($day) ); ?>                
                    </span> <!-- end .day -->
                    <span class="date_sidebar">                    
                    <?php echo date( 'F j', strtotime($day) ); ?>
                    </span><!-- end .date -->
                </h1>
            </li>

        <?php

        foreach($list as $ev){

            $time_value = $ev[0]; $post_id = $ev[1]; $time_key = $ev[2];
            $e = (object) $events[$post_id];

            if ($time_key == 'opening_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Opening</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->opening_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->opening_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

            <?php } else if ($time_key == 'closing_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Closing</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                       <h5><?php echo date( $convertedtime, strtotime( $e->closing_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->closing_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

            <?php } else if ($time_key == 'artist_talk_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Artist Talk</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->artist_talk_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->artist_talk_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

            <?php } else if ($time_key == 'special_event_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Special Event</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->special_event_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->special_event_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

             <?php } else if ($time_key == 'lecture_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Lecture</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->lecture_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->lecture_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

             <?php } else if ($time_key == 'panel_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Panel</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->panel_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->panel_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

             <?php } else if ($time_key == 'workshop_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Workshop</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->workshop_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->workshop_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->
            <?php }
        }

        echo "</ul>"; // End a day

    endif; endforeach;



endif; // END $event_query->have_posts();

?>

I have a page that loops through a custom post type and sorts by dates entered in meta_boxes. It seemed to work at a reasonable speed before but now it is extremely slow. There is probably some logical mistake I am making in the order of my querys and php requests but I do not know how to optimize it.

Here is the code that is loaded on the page.

Link to *original*code on Pastebin for easier viewing: Pastebin Code

Here is the code from the functions.php that makes the meta data for the posts. Link to Pastebin of this code section: Meta Box Code

Thank you in advance.

EDIT 12/14/2011 - Replaced original code with example below. Am I on the right track? Original code still available at Pastebin Link.

        <?php
    /* Let's get all the meta date in one call rather then so many queries */

    $event_custom_meta=get_post_custom($post->ID); // Get all the data 

    $event_start_date = $event_custom_meta['start_date'][0];  /* Not sure why I need [0] here but it returns Array if I don't have it */
    $event_end_date = $event_custom_meta['end_date'][0];
    $event_opening_time = $event_custom_meta['opening_time'][0];
    $event_closing_time = $event_custom_meta['artist_closing_time'][0];
    $event_lecture_time = $event_custom_meta['lecture_time'][0];
    $event_panel_time = $event_custom_meta['panel_time'][0];
    $event_special_event_time = $event_custom_meta['special_event_time'][0];
    $event_workshop_time = $event_custom_meta['workshop_time'][0];
    $event_event_venue = $event_custom_meta['event_venue'][0];
    $event_custom_event_venue = $event_custom_meta['custom_event_venue'][0];

    echo $event_start_date;
    echo $event_end_date;
    echo $event_opening_time;
    echo $event_closing_time;
    echo $event_artist_talk_time;
    echo $event_lecture_time;
    echo $event_panel_time;
    echo $event_special_event_time;
    echo $event_workshop_time;
    echo $event_event_venue;

    /* The test works. Alright, now I can echo these variables anyplace that I want */

    /* Replace the previous code below with the cleaner code after*/
    ?>
    <!-- old code -->
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>

    <a href=""><h3><?php  

        if (get_post_meta(get_the_ID(),'event_venue', true) != 'other') { 
            echo get_post_meta(get_the_ID(),'event_venue', true);
            }
        if (get_post_meta(get_the_ID(),'event_venue', true) == 'other') {
            echo get_post_meta(get_the_ID(),'custom_event_venue', true);
            }
    ?></h3></a> 


    <!-- new code -->
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>

    <a href=""><h3><?php  

        if ($event_event_venue != 'other') { 
            echo $event_event_venue;
            }
        if ($event_event_venue == 'other') {
            echo $event_custom_event_venue;
            }
    ?></h3></a> 
    <!-- end new code -->

   <?php 
   /* is calling the function get_related_event data another query?  
   *Is there away to add this infor to the orignial meta_box setup?
   */
   echo get_related_event_data( $event_event_venue, 'address' ); 
   echo get_related_event_data( $event_event_venue, 'phone_no' ); 
   echo get_related_event_data( $event_event_venue, 'url' ); 
   /* end question */
   ?>

END EDIT on 12/14/2011 - added revised code example above

I am pretty sure that it is in this section below, because the page speeds up when I take out the "Upcoming" loop. What am I doing wrong?

Link to Suspicious section on Pastebin:Suspicious Code

<h1>Upcoming</h1>

  <?php
  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();  // put before the $event_query and seems to work

  $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();  

  $keys = array('opening_time', 'closing_time', 'artist_talk_time', 'special_event_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;
  ?>

  <ul>

  <?php
  if ($counttest >0) {
  ?>
  <li>
  <h1 class="upcoming_date">
  <span class="total">
  <?php 
      echo $counttest;
      if ($counttest>=2) {
          echo ' Events ';
      } // end if ($totalevents>=2) function
      if ($counttest<2) { 
          echo ' Event ';
      }
  ?>

  </span> <!-- end .total -->
  <span class="day_sidebar">

  <?php
      if ($thedate==$today) {
          echo 'Today';
      }
      if ($thedate>$today) {
          echo $thedaytext;
      }
  ?>

  </span> <!-- end .day -->
  <span class="date_sidebar">

  <?php
      echo $thedatetext;
  ?>
  </span><!-- end .date -->
  </h1>
  </li>

  <?php
  }
  endif;
  $counttest=0;

  sort($times); // I changed the asort to sort here

       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') { ?>

      <li class="sidebar_event">
      <a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_post_thumbnail($post_id, 'upcoming_event_sidebar'); ?></a> 

      <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_title($post_id); ?></a></h2> 
      <h3>Opening</h3>
      <h4>

   <?php
      if (get_post_meta($post_id,'event_venue', true) != 'other') { 
          echo get_post_meta($post_id,'event_venue', true);
          }
      if (get_post_meta($post_id,'event_venue', true) == 'other') {
          echo get_post_meta($post_id,'custom_event_venue', true);
          }

   ?> 
   </h4>
   <h5><?php $opening_time_formated = date($convertedtime, strtotime( get_post_meta($post_id,'opening_time', true)));
        echo $opening_time_formated;?> </h5>
            <hr />
      </li><!-- end .sidebar_event -->
      <?php }

      else if ($time_key == 'artist_talk_time') { ?>

      <li class="sidebar_event">
      <a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_post_thumbnail($post_id, 'upcoming_event_sidebar'); ?></a> 

      <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_title($post_id); ?></a></h2> 
      <h3>Artist Talk</h3>
      <h4><?php echo get_post_meta($post_id,'event_venue', true);?> </h4>
      <h5><?php $artist_talk_time_formated = date($convertedtime, strtotime( get_post_meta($post_id,'artist_talk_time', true)));
        echo $artist_talk_time_formated;?>  </h5>
      <hr />
      </li><!-- end .sidebar_event -->
      <?php }

       else if ($time_key == 'closing_time') { ?>
       <li class="sidebar_event">
       <a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_post_thumbnail($post_id, 'upcoming_event_sidebar'); ?></a> 

      <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_title($post_id); ?></a></h2> 
      <h3>Closing</h3>
      <h4><?php echo get_post_meta($post_id,'event_venue', true);?> </h4>
      <h5><?php echo get_post_meta($post_id,'closing_time', true);?> </h5>
      <hr />
      </li><!-- end .sidebar_event -->
      <?php }

       else if ($time_key == 'special_event_time') { ?>
       <li class="sidebar_event">
       <a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_post_thumbnail($post_id, 'upcoming_event_sidebar'); ?></a> 

      <h2><a href="<?php echo get_permalink($post_id) ?>" title="<?php echo get_the_title($post_id); ?>"> <?php echo get_the_title($post_id); ?></a></h2> 
      <h3>Special Event</h3>
      <h4><?php echo get_post_meta($post_id,'event_venue', true);?> </h4>
      <h5><?php $special_event_time_formated = date($convertedtime, strtotime( get_post_meta($post_id,'special_event_time', true)));
        echo $special_event_time_formated;?></h5>
      <hr />
      </li><!-- end .sidebar_event -->
      <?php }

       }

        rewind_posts();
  ?>

  </ul>

  <?php
   }
   ?>
   <br />
  <br />
  </div>
3
  • 1
    y not simply make the future value '+10 days' and remove the for($i) loop? That will reduce your wp database queries from 10 to 1.
    – Ijaas
    Dec 13, 2011 at 14:12
  • @ijaas Thank you for the quick reply. I thought I needed the for(i$) loop to grab the events for each individual day and only the events for that day. If I set the future value to +10 days, won't that only display events from the 10th day? Dec 13, 2011 at 14:47
  • 1
    it can be done with one db query and without the for() loop. Will write a more detailed answer by the end of the day.
    – Ijaas
    Dec 14, 2011 at 4:46

2 Answers 2

1

sorry about the delay. Here is the condensed query that searchs for posts between today and 10 days into the future, then sorts and displays in them order.

theoretically it should work but let me know if u get any errors.

Update: 12/22/2012 - Fixed Sorting

<h1>Upcoming</h1>
<?php

$convertedtime = "Y-m-d H:i"; // Time format You can remove this if it is defined before
$convertedtime = "g:i"; // convert to 12 hour clock and minutes for upcomming events sidebar
$convertedendtime = "g:i a"; // convert to 12 hour clock and minutes for upcomming events sidebar
$today  = date ( 'Y-m-d H:i' ); 
$thedate = time(); // IF you want to start from a future date use strtotime( FutureDateHere );
$thedate = date ( 'Y-m-d H:i' , $thedate );
$future = strtotime ( '+10 Days' ); // IF you want to start from a future date use strtotime( '+10 Days', strtotime( FutureDateHere ) );
$future = date ( 'Y-m-d H:i' , $future );

$times  = array();
$events = array();

$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' => 'NONE',*/
    array(
        'key' => $keys,
        '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(); 

    // Storing events in array is more efficent than using get_the_title, ect... later on
    $events[$post->ID] = array(
        'title'     => apply_filters('the_title', $post->post_title),
        'link'      => get_permalink($post->ID),
        'thumbnail' => get_the_post_thumbnail($post->ID, 'upcoming_event_sidebar'),
        'venue'     => get_post_meta($post->ID,'event_venue', true),
        'custom_venue' => get_post_meta($post->ID,'custom_event_venue', true),
        'opening_time_end' => get_post_meta($post->ID,'opening_time_end', true),
        'closing_time_end' => get_post_meta($post->ID,'closing_time_end', true),
        'artist_talk_time_end' => get_post_meta($post->ID,'artist_talk_time_end', true),
        'special_event_time_end' => get_post_meta($post->ID,'special_event_time_end', true),
        'lecture_time_end' => get_post_meta($post->ID,'lecture_time_end', true),
        'panel_time_end' => get_post_meta($post->ID,'panel_time_end', true),
        'workshop_time_end' => get_post_meta($post->ID,'workshop_time_end', true)
    );


    $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) {
                $times[strtotime($custom_field_value)][] = array($custom_field_value, $post->ID, $custom_field_key);  
                $events[$post->ID][$custom_field_key] = $custom_field_value; //opening_time, closing_time.......

            }
        }
    }

endwhile;

    ksort($times);

    foreach($times as $day => $list): if($num = count($list)):

        sort($list);

        echo "<ul>"; // Start a day
        ?>
            <li class="sidebar_event_top">
                <h1>
                    <span class="total">
                        <?php echo $num.(($num >= 2)? " Events " : " Event "); ?>                    
                    </span> <!-- end .total -->
                    <span class="day_sidebar">
                        <?php echo ($day == $today)? "Today" : date( 'l', $day ); ?>                
                    </span> <!-- end .day -->
                    <span class="date_sidebar">                    
                    <?php echo date( 'F j', $day ); ?>
                    </span><!-- end .date -->
                </h1>
            </li>

        <?php

        foreach($list as $ev){

            $time_value = $ev[0]; $post_id = $ev[1]; $time_key = $ev[2];
            $e = (object) $events[$post_id];

            if ($time_key == 'opening_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Opening</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->opening_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->opening_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

            <?php } else if ($time_key == 'closing_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Closing</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                       <h5><?php echo date( $convertedtime, strtotime( $e->closing_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->closing_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

            <?php } else if ($time_key == 'artist_talk_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Artist Talk</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->artist_talk_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->artist_talk_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

            <?php } else if ($time_key == 'special_event_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Special Event</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->special_event_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->special_event_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

             <?php } else if ($time_key == 'lecture_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Lecture</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->lecture_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->lecture_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

             <?php } else if ($time_key == 'panel_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Panel</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->panel_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->panel_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->

             <?php } else if ($time_key == 'workshop_time') { ?>

                <li class="sidebar_event">
                    <a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->title ?></a></h2>
                    <h3>Workshop</h3>
                    <h4><?php echo ($e->venue != 'other')? $e->venue : $e->custom_venue; ?></h4>
                    <h5><?php echo date( $convertedtime, strtotime( $e->workshop_time ) ); echo " - "; echo date( $convertedendtime, strtotime( $e->workshop_time_end ) ) ;?></h5>

                </li><!-- end .sidebar_event -->
            <?php }
        }

        echo "</ul>"; // End a day

    endif; endforeach;



endif; // END $event_query->have_posts();

?>
14
  • 1
    oops, yes u can use get_post_meta($post->ID,'opening_time', true) instead of $post->post_date
    – Ijaas
    Dec 19, 2011 at 1:18
  • 1
    try replace $times[$day][] = array(... with $times[date('Y-m-d', strtotime($custom_field_value))][] = array(...
    – Ijaas
    Dec 19, 2011 at 8:22
  • 1
    yes the $day needs to be defined for use in the first <h1> tag. The get_post_meta function does not make a db call, as all meta values for the posts are cached by the WP_Query function so it isn't a problem to make multiple get_post_meta queries.
    – Ijaas
    Dec 20, 2011 at 1:14
  • 1
    adding sort($times); before the foreach loop should do the trick. If it doesn't i will look into it and update my answer.
    – Ijaas
    Dec 20, 2011 at 13:07
  • 1
    i updated my answer with what should fix the sorting of days.
    – Ijaas
    Dec 22, 2011 at 9:25
1

Two things I see that will very likely speed this up.

Use the native time parameters in WP Query first and then do your custom "stuff": http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

Use get_post_custom instead of get_post_meta: http://codex.wordpress.org/Function_Reference/get_post_custom

8
  • @Wrck Thank you for the reply. To clarify, I am not trying to show posts with a publish date in a certain range, I am show posts that have a meta_key that is a text string date that falls withing a certain range. It seems that the WP time parameters are for post dates and not meta_keys. Am I wrong in thinking this? I tried to replace get_post_meta' with 'get_post_custom but got errors. I will continue to read the Codex to figure it out. I am still not sure how to stream line the time parameters either but will try my best. Suggestions and hints are welcome. Thank you. Dec 13, 2011 at 20:51
  • I understand now that I need to use get_post_custom That should lower the DB queries. The problem is I am not sure how to implement it efficiently. Currently it just echos Array any time I use it. I imagine that I need to use get_post_custom and have it set all the met_keys to a set of variables and then use the variables in the if statements to display the content in the right order. Still wracking my brain. Dec 14, 2011 at 2:39
  • Ya basically returning an array is 1 db query as opposed to many, then you have to parse the array though. As for 'the time', since they are "custom dates", I was somewhat confused. I think to optimize this you might not want to churn through all that cpu on one page load and possibly store some of this data in the transient API, especially of this data does not change more than a few times every hour.
    – Wyck
    Dec 14, 2011 at 4:16
  • Thanks for the explanation. I am still having issues outputing everything. I can get a large array to print but it has a lot of extra information. I should be able to figure it out and get it at least a bit more efficient. I am not sure what a "transient API" is so I will be looking that up and trying to implement it. That would be great if it stored the info once every couple of hours. Now how to do it? Dec 14, 2011 at 11:19
  • I added a [0] to the end of the get_post_custom and it is grabbing the information now. I posted an example variable set up at the top of this page. Is that what I am supposed to do? Thanks again. Dec 14, 2011 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.