1

I saw a request for a simple 7 day event calendar that always starts with today.

I have a custom meta box called opening-time that gets a YYYY-MM-DD input.

For now this is what I have, and its not working.

It only outputs 2011-11-17 today is 11-11-2011 so that is wrong. It also must have a php error because none of the blog outputs either.

Thank you in advance.

<ul id="days_header">
<?php
    $today = date("o-m-d");
 $future = strtotime ( '+6 days' , strtotime ( $today ) ) ;
 $future = date ( 'o-m-d' , $future );
 $event_query = new WP_Query(
array( 
  'post_type'   => 'event',
  'meta_key'    => 'opening_time',
  'orderby'     => 'meta_value',
  'order'       => 'asc',
  'meta_query'  => array(
     array(
      'key'     => 'opening_time',
      'value'   => array($today,$future),
      'compare' => 'BETWEEN',
      'type'    => 'DATE'
    )
   )
  ) 
);
?>
<?php for ($i=0; $i<7; $i++):
$thedate = strtotime ( '+'.$i.' day' , strtotime ( $today ) ) ;
$thedate = date ( 'o-m-d' , $thedate ); ?>

<li>
<?php echo $thedate; ?>
<?php if ($event_query->have_posts()) : while ($event_query->have_posts()) :  $event_query->the_post();?>
  <?php if(meta('opening_time') == $thedate) :?>
    <?php the_title(); ?>
    <?php else: ?>
      No Events Today

  <?php endif; ?>
  </li>
<?php endwhile; endif; rewind_posts(); ?> 


1 Answer 1

1

I haven't had much success in the past using the BETWEEN comparison with meta queries. You might have better luck by replacing your meta query with a >= and <= comparison:

'meta_query'  => array(
   array(
    'key'     => 'opening_time',
    'value'   => $today,
    'compare' => '>=',
    'type'    => 'DATE'
  ),
  array(
    'key'     => 'opening_time',
    'value'   => $future,
    'compare' => '<=',
    'type'    => 'DATE'
  )
)

Depending on your application you may want to tune the results a bit by dropping the "or equal to" from one or both of the statements (right now it's being very forgiving).

As for the PHP error, the code you provided doesn't end the "for" loop with an endfor. Have you checked your PHP error log? You can also try turning on WP_DEBUG in your wp-config.php file.

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.