Advertisement
johnbentwin

Custom Post Type Sort

Nov 18th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.31 KB | None | 0 0
  1.       <?php
  2.           $today = date("Y-m-d 00:00");  //set today's date
  3.           $todaytext = date("l"); // define $todaytext with written out days
  4.              
  5.       for ($i=0; $i<7; $i++){
  6.           $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
  7.           $thedate = date ( 'Y-m-d H:i' , $thedate );
  8.           $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
  9.           $thedatetext = date ( 'l' , $thedatetext );
  10.           $future = strtotime ( '+24 hours' , strtotime ( $thedate ) ) ;
  11.           $future = date ( 'Y-m-d H:i' , $future );
  12.      
  13.      $args = array(
  14.      'post_type' => 'event',
  15.      'orderby'     => 'meta_value',
  16.      'order'       => 'asc',
  17.      'meta_query' => array(
  18.      'relation' => 'OR',
  19.         array(
  20.             'key' => 'opening_time',
  21.             'value' => array($today,$future),
  22.             'compare' => 'BETWEEN',
  23.             'type' => 'DATE'
  24.         ),
  25.         array(
  26.             'key' => 'artist_talk_time',
  27.             'value' => array($today,$future),
  28.             'compare' => 'BETWEEN',
  29.             'type' => 'DATE'   
  30.         )
  31.     )
  32.     );
  33.  
  34.     $event_query = new WP_Query( $args );
  35.  
  36.     if ($event_query->have_posts()) : while ($event_query->have_posts()) :  $event_query->the_post();    
  37.    
  38.     $keys = array('opening_time', 'film_time', 'artist_talk_time');
  39.     $times = array();  // I tried putting the new array line here but then I got a repeat of the post for everyday
  40.     $custom_field_keys = get_post_custom_keys();
  41.     foreach ($custom_field_keys as $custom_field_key) {
  42.    
  43.     if (in_array($custom_field_key, $keys))
  44.     {
  45.         $custom_field_value = get_post_meta($post->ID, $custom_field_key, true);
  46.        
  47.         if ($custom_field_value >= $thedate && $custom_field_value <= $future) {
  48.            
  49.             $counttest++;
  50.             $times[] = array($custom_field_value, $post->ID, $custom_field_key); // I changed the array here
  51.        
  52.         }
  53.         }
  54.         }    
  55.    
  56.       endwhile;
  57.      
  58.      if ($counttest >0) {
  59.       echo $counttest;
  60.      
  61.       if ($counttest>=2){
  62.       echo ' Events ';
  63.       } // end if ($totalevents>=2) function
  64.      
  65.       if ($counttest<2){
  66.       echo ' Event ';
  67.       }
  68.  
  69.       if ($thedate==$today){echo 'Today';}
  70.       if ($thedate>$today){
  71.       echo $thedatetext;}
  72.       echo  '<br>';
  73.    
  74.       }
  75.       endif;
  76.       $counttest=0;
  77.  
  78.  
  79.       sort($times); // I changed the asort to sort here
  80.      
  81.      foreach ($times as $event) { $time_value = $event[0]; $post_id = $event[1]; $time_key = $event[2];  // changed the foreach here
  82.  
  83.     if ($time_key == 'opening_time') { ?>
  84.    
  85.     <li class="sidebar_event">
  86.     <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>
  87.    
  88.     <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>
  89.     <h3>Opening</h3>
  90.     <h4><?php echo get_post_meta($post_id,'event_venue', true);?> </h4>
  91.     <h5><?php echo get_post_meta($post_id,'opening_time', true);?> </h5>
  92.     </li><!-- end .sidebar_event -->
  93.     <?php }
  94.    
  95.     else if ($time_key == 'artist_talk_time') { ?>
  96.          
  97.     <li class="sidebar_event">
  98.     <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>
  99.    
  100.     <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>
  101.     <h3>Artist Talk</h3>
  102.     <h4><?php echo get_post_meta($post_id,'event_venue', true);?> </h4>
  103.     <h5><?php echo get_post_meta($post_id,'artist_talk_time', true);?> </h5>
  104.     </li><!-- end .sidebar_event -->
  105.     <?php }
  106.      
  107.      else if ($time_key == 'film_time') { ?>
  108.      <li class="sidebar_event">
  109.      <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>
  110.    
  111.     <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>
  112.     <h3>Artist Talk</h3>
  113.     <h4><?php echo get_post_meta($post_id,'event_venue', true);?> </h4>
  114.     <h5><?php echo get_post_meta($post_id,'artist_talk_time', true);?> </h5>
  115.     </li><!-- end .sidebar_event -->
  116.     <?php }
  117.  
  118.      }
  119.    
  120.       rewind_posts();
  121.      
  122.       }
  123.       ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement