Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

4th EDIT 11/17/2011

I spoke too soon. Now I have all the posts showing up but the total events does not seperate the events by day after I moved the endif; and endwhile; I posted the modified code below and below this is the previously modified code.

Before the count was correct for each day. If I had two events from different posts on Friday then it would say "2 Events on Friday" but it would only display the content of the most recent post.

Now it displays the content of all the posts but if I have two events from different posts on Friday then it say "1 Event Friday" before each post.

Here is the code and Thank you.

<?php
      $today = date("Y-m-d 00:00");  //set today's date 
      $todaytext = date("l"); // define $todaytext with written out days

  for ($i=0; $i<7; $i++){
      $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
      $thedate = date ( 'Y-m-d H:i' , $thedate );
      $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( 'l' , $thedatetext );
      $future = strtotime ( '+24 hours' , strtotime ( $thedate ) ) ;
      $future = date ( 'Y-m-d H:i' , $future ); 

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

$event_query = new WP_Query( $args ); 

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

$keys = array('opening_time', 'film_time', 'artist_talk_time');
$times = array();  // I tried putting the new array line here but then I got a repeat of the post for everyday
$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); // I changed the array here

    }
    }
    }     


 if ($counttest >0) {
  echo $counttest;

  if ($counttest>=2){
  echo ' Events ';
  } // end if ($totalevents>=2) function

  if ($counttest<2){ 
  echo ' Event ';
  }
  echo $count;
  if ($thedate==$today){echo 'Today';}
  if ($thedate>$today){
  echo $thedatetext;}
  echo  '<br>';

  }

  $counttest=0;


  asort($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 echo get_post_meta($post_id,'event_venue', true);?> </h4>
<h5><?php echo get_post_meta($post_id,'opening_time', true);?> </h5>
</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 echo get_post_meta($post_id,'artist_talk_time', true);?> </h5>
</li><!-- end .sidebar_event -->
<?php }

  else if ($time_key == 'film_time') {
        ?>
  <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> 
  <h2>Film</h2>
  <?php
  echo get_post_meta($post_id,'film_time', true); echo get_the_content($post_id); 
 }

 }
     endwhile;
  endif;

  rewind_posts();

  }
  ?>

End 4th EDIT

3rd EDIT 11/17/2011 I made the changes that you suggested Marcochiesi but the behavior stays the same. I tried making any other changes that I could wrap my heard around but know luck. What would you try next? Thank you so much for your help.

revised code

  <?php
      $today = date("Y-m-d 00:00");  //set today's date 
      $todaytext = date("l"); // define $todaytext with written out days

  for ($i=0; $i<7; $i++){
      $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
      $thedate = date ( 'Y-m-d H:i' , $thedate );
      $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( 'l' , $thedatetext );
      $future = strtotime ( '+24 hours' , strtotime ( $thedate ) ) ;
      $future = date ( 'Y-m-d H:i' , $future ); 

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

$event_query = new WP_Query( $args ); 

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

$keys = array('opening_time', 'film_time', 'artist_talk_time');
$times = array();  // I tried putting the new array line here but then I got a repeat of the post for everyday
$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); // I changed the array here

    }
    }
    }     
 endwhile;

 if ($counttest >0) {
  echo $counttest;

  if ($counttest>=2){
  echo ' Events ';
  } // end if ($totalevents>=2) function

  if ($counttest<2){ 
  echo ' Event ';
  }
  echo $count;
  if ($thedate==$today){echo 'Today';}
  if ($thedate>$today){
  echo $thedatetext;}
  echo  '<br>';
  }

  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') {
    ?>
  <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> 
  <h2>Opening</h2>
  <?php
  echo get_post_meta($post_id,'opening_time', true); echo get_the_content($post_id); 

  }
  else if ($time_key == 'artist_talk_time') {
        ?>
  <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> 
  <h2>Artist Talk</h2>
  <?php
  echo get_post_meta($post_id,'artist_talk_time', true); echo get_the_content($post_id); 

 }
  else if ($time_key == 'film_time') {
        ?>
  <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> 
  <h2>Film</h2>
  <?php
  echo get_post_meta($post_id,'film_time', true); echo get_the_content($post_id); 
 }

 }

  rewind_posts();

  }
  ?>

end 3rd EDIT

2nd EDIT 11/16/2011 Marcochiesi, thanky for the additional help. Now I get the right count of events and they are sorted properly. The problem is that only the most recent post shows any content. If I add a new CPT event then it will show the its title and permalink but if I delete it it goes back to the next most recent post.

Do you know what the problem is? Thank you again. It is impossible without your help.

2nd revised code

  <?php
      $today = date("Y-m-d 00:00");  //set today's date 
      $todaytext = date("l"); // define $todaytext with written out days

  for ($i=0; $i<7; $i++){
      $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
      $thedate = date ( 'Y-m-d H:i' , $thedate );
      $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( 'l' , $thedatetext );
      $future = strtotime ( '+24 hours' , strtotime ( $thedate ) ) ;
      $future = date ( 'Y-m-d H:i' , $future ); 

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

$event_query = new WP_Query( $args ); 

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

$keys = array('opening_time', 'film_time', 'artist_talk_time');
$times = array();
$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[$custom_field_key] = array($custom_field_value, $post->ID);
    }
    }
    }     
 endwhile;
 if ($counttest >0) {
  echo $counttest;

  if ($counttest>=2){
  echo ' Events ';
  } // end if ($totalevents>=2) function

  if ($counttest<2){ 
  echo ' Event ';
  }
  echo $count;
  if ($thedate==$today){echo 'Today';}
  if ($thedate>$today){
  echo $thedatetext;}
  echo  '<br>';
  }

  endif;
   $counttest=0;


 asort($times);
 foreach ($times as $time_key => $event) {
 $time_value = $event[0]; $post_id = $event[1]; 

 if ($time_key == 'opening_time') {
    ?>
  <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> 
  <h2>Opening</h2>
  <?php
  echo get_post_meta($post_id,'opening_time', true); echo get_the_content($post_id); 

 }
 else if ($time_key == 'artist_talk_time') {
        ?>
  <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> 
  <h2>Artist Talk</h2>
  <?php
  echo get_post_meta($post_id,'artist_talk_time', true); echo get_the_content($post_id); 

 }
  else if ($time_key == 'film_time') {
        ?>
  <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> 
  <h2>Film</h2>
  <?php
  echo get_post_meta($post_id,'film_time', true); echo get_the_content($post_id); 
 }

 }

  rewind_posts();

  }
  ?>

END 2nd EDIT

EDIT 11/15/2011

After changing the code from marcochiesi (thank you) I was able to stream line my if statements and gain the ability to sort artist_talk_time in front of opening_time if that is what the meta_key said.

But now I have duplicate posts coming from multiple days in the sorting. Each CPT can have 3 meta_keys that have a date & time value.

Currently I have 3 test CPTs. 1 has an opening_time at 2:00 today (it shows up first) It also has an artist talk time on the next day at 2:00. This also shows up but in both today and tomorrow.

This behavior happens with all the posts. What am I doing wrong?

revised code

 <?php
      $today = date("Y-m-d 00:00");  //set today's date 
      $todaytext = date("l");  // define $todaytext with written out days

 for ($i=0; $i<7; $i++){
      $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
      $thedate = date ( 'Y-m-d H:i' , $thedate );
      $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( 'l' , $thedatetext );
      $future = strtotime ( '+23 hours' , strtotime ( $thedate ) ) ;
      $future = date ( 'Y-m-d H:i' , $future ); 
 $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'    
    )
)
);

$event_query = new WP_Query( $args ); 

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

$keys = array('opening_time', 'film_time', 'artist_talk_time');
$times = array();
$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[$custom_field_key] = $custom_field_value;
    }
 }
 }     
 endwhile;
 if ($counttest >0) {
  echo $counttest;

  if ($counttest>=2){
  echo ' Events ';
  } // end if ($totalevents>=2) function

  if ($counttest<2){ 
  echo ' Event ';
  }
  echo $count;
  if ($thedate==$today){echo 'Today';}
  if ($thedate>$today){
  echo $thedatetext;}
  echo  '<br>';
  }

  endif;

  $counttest=0;

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

  asort($times);
  foreach ($times as $time_key => $time_value) {

  if ($time_key == 'opening_time') {
    ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2>Opening</h2>

  <?php
  echo get_post_meta($post->ID,'opening_time', true);
  the_content();
  }
  else if ($time_key == 'artist_talk_time') {
        ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2>Artist Talk</h2>

  <?php
  echo get_post_meta($post->ID,'artist_talk_time', true);
  the_content();
  }
  else if ($time_key == 'film_time') {
        ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2>Artist Talk</h2>

  <?php
  echo get_post_meta($post->ID,'film_time', true);
  the_content();
  }

  }

  endwhile;

  endif;
  rewind_posts();

  }
  ?>

END EDIT 11/15/2011 - Original Question Below

Through help on this answers collection I have the code working below that sorts events by date and then lists them and counts them. It does not filter by time.

For example, a single CPT 'event' has multiple meta_keys with a yyyy-mm-dd 00:00 entry. Each main event or art opening runs for about a month, but there is also the opening reception and artist talks that have their own meta_keys opening_time and artist_talk_time.

I have another page that lists the month long portion of the events. Now I am trying to list the upcoming events in order.

The problem is in an individual CPT "event" if the artist_talk_time is 16:00 and the opening_time is after 16:00, the opening_time shows first since it is called first in the loop where I display the_content(). This is not the behavior that I want.

All events on a single day, even if in the same CPT should be listed in order by time.

I am sure that I am doing this the wrong way and any help is greatly appreciated.

Thank you.

<?php
      $today = date("Y-m-d 00:00");
      $todaytext = date("l");

  for ($i=0; $i<7; $i++){
      $thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
      $thedate = date ( 'Y-m-d H:i' , $thedate );
      $thedatetext = strtotime ( '+'.$i.' day' , strtotime ( $todaytext ) ) ;
      $thedatetext = date ( 'l' , $thedatetext );
      $future = strtotime ( '+23 hours' , strtotime ( $thedate ) ) ;
      $future = date ( 'Y-m-d H:i' , $future ); 
 $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'    
    )
)
);

 $event_query = new WP_Query( $args );      

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

if ((get_post_meta($post->ID,'opening_time', true)>=$thedate) && (get_post_meta($post->ID,'opening_time', true)<=$future))
  { $counttest++;}

if ((get_post_meta($post->ID,'film_time', true)>=$thedate) && (get_post_meta($post->ID,'_time', true)<=$future))
  { $counttest++;}

if ((get_post_meta($post->ID,'artist_talk_time', true)>=$thedate) && (get_post_meta($post->ID,'artist_talk_time', true)<=$future))
  { $counttest++;}

endwhile;
  if ($counttest >0) {
  echo $counttest;

  if ($counttest>=2){
  echo ' Events ';
  } // end if ($totalevents>=2) function

  if ($counttest<2){ 
  echo ' Event ';
  }
  echo $count;
  if ($thedate==$today){echo 'Today';}
  if ($thedate>$today){
  echo $thedatetext;}
  echo  '<br>';
  }

  endif;

  $counttest=0;

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

  if ((get_post_meta($post->ID,'opening_time', true)>=$thedate) && (get_post_meta($post->ID,'opening_time', true)<=$future))
  {
  ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2>Opening Reception</h2>
  <?php
  echo get_post_meta($post->ID,'opening_time', true);
  the_content();

  }

  if ((get_post_meta($post->ID,'artist_talk_time', true)>=$thedate) && (get_post_meta($post->ID,'artist_talk_time', true)<=$future))
  { ?>
  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>
  <h2> Artist Talk </h2>
  <?php 
  echo get_post_meta($post->ID,'artist_talk_time', true);
  the_content();

  } 

  endwhile;

  endif;
  rewind_posts();

  }
  ?>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Just an idea: you could save your time values in an array and then sort it with PHP. Example:

$keys = array('opening_time', 'film_time', 'artist_talk_time');
$times = array();
$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[$custom_field_key] = $custom_field_value;
        }
    }
}
asort($times);
foreach ($times as $time_key => $time_value) {
    if ($time_key == 'opening_time') {
        ...
    }
    else if ($time_key == 'film_time') {
        ...
    }
    ...
}
share|improve this answer
    
@marchochesi Thank you. Question: This would go after the first if ($event_query->have_posts()) : while ($event_query->have_posts()) : $event_query->the_post(); correct? Then between ` if ($time_key == 'opening_time') { ... }` I put the_content(); correct? Also I am getting a syntax error on the line ` if (in_array($custom_field_key, $keys) { ` Since the data is in a meta box should I use 'meta_key' instead of 'custom_key'? Will be trying every combination tonight. Thank you again for the clarification. –  John Bentwin Nov 14 '11 at 18:27
    
Yes, the code goes inside the loop, after the first if ($event_query->have_posts()).... My code was a bit simplified, it was just an idea... I think you should save the post ids in an array too and then use a get_the_content([post id]) in place of the_content(). The syntax error is caused by a missing ) at the end of the if. –  marcochiesi Nov 14 '11 at 18:47
    
@marchochesi I figured out the syntax issue. It was missing a ` C` and should read ` if (in_array($custom_field_key, $keys)) ` Now I just have to figure out where the if ($event_query->have_posts()) : while ($event_query->have_posts()) : $event_query->the_post(); goes. Thank you again and I will keep working on it. I must have been typing while you answered. –  John Bentwin Nov 14 '11 at 18:53
    
@marchochesi Thank you again for your help. I now have the posts sorting by time but the dates are messed up. I am getting duplicate content in each day. Is it something obvious? Thank you for the advice. –  John Bentwin Nov 15 '11 at 12:05
    
I think you should avoid the second while loop (just use the foreach) and save the post ids for later use: $times[$custom_field_key] = array($custom_field_value, $post->ID); ... foreach ($times as $time_key => $event) { $time_value = $event[0]; $post_id = $event[1]; ... if ($time_key == 'opening_time') { ?> <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> ... echo get_post_meta($post_id,'opening_time', true); echo get_the_content($post_id); } –  marcochiesi Nov 16 '11 at 2:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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