Very Slow Page – How to Optimize # of Queries?

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="https://wordpress.stackexchange.com/questions/36331/<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="https://wordpress.stackexchange.com/questions/36331/<?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="https://wordpress.stackexchange.com/questions/36331/<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="https://wordpress.stackexchange.com/questions/36331/<?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="https://wordpress.stackexchange.com/questions/36331/<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="https://wordpress.stackexchange.com/questions/36331/<?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="https://wordpress.stackexchange.com/questions/36331/<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="https://wordpress.stackexchange.com/questions/36331/<?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="https://wordpress.stackexchange.com/questions/36331/<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="https://wordpress.stackexchange.com/questions/36331/<?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="https://wordpress.stackexchange.com/questions/36331/<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="https://wordpress.stackexchange.com/questions/36331/<?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="https://wordpress.stackexchange.com/questions/36331/<?php echo $e->link ?>" title="<?php echo $e->title ?>"> <?php echo $e->thumbnail ?></a>                        
                    <h2><a href="https://wordpress.stackexchange.com/questions/36331/<?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();

?>