• Resolved TrishaM

    (@trisham)


    Searched all the posts here related to using custom event dates in queries and didn’t find an answer to my specific problem, hoping someone can help.

    I have a custom post type for events, with custom fields to hold the event start date and end date.

    I have query that finds all matching posts and lists them in order of their start date as long as the start date is a future date (old events don’t show), however I have two problems:

    The events drop off on the date that they start even though I’m using <= in my query, not just < (they should display through that date and drop off on the following date); and

    For multi-day events, they should remain in the list until the event has ended (the day after the date the event ends) but they still drop off the list on the date that they start;

    Here’s my code:

    global $post;
    
    $eventdate = get_post_meta($post->ID,'event_date_start',true);
    $eventdate2 = get_post_meta($post->ID,'event_date_end',true);
    $eventlocation = get_post_meta($post->ID,'event_location',true);
    $eventaddress = get_post_meta($post->ID,'event_location_address',true);
    $eventdirections = get_post_meta($post->ID,'event_location_directions',true);
    $eventcontact = get_post_meta($post->ID,'event_contact',true);
    $eventcontactph = get_post_meta($post->ID,'event_contact_phone',true);
    $eventcontactem = get_post_meta($post->ID,'event_contact_email',true);
    $eventnumdays = get_post_meta($post->ID,'event_number_of_days',true);
    
    $myposts =           get_posts('post_type=event&meta_key=event_date_start&orderby=meta_value_num&order=ASC&posts_per_page=-1');
    foreach ( $myposts as $post ) : setup_postdata( $post );
    
    $today = time();
      if ( $eventnumdays == "Multiple Days" ) {
            $exp_date = strtotime(get_post_meta($post->ID,'event_date_end',true));
      } else {
            $exp_date = strtotime(get_post_meta($post->ID,'event_date_start',true));
      }
    if ($today <= $exp_date) {

    If the custom field for the event_number_of_days is set to Single Day, then there is no custom field created for the end date, so my attempts to use event_date_end failed, which is why I’m using the custom field for number of days instead to determine if the post should expire after the start date or after the end date.

    This query does display the events correctly, just not >>through<< the date they need to be still on the list….

    For example, I have an event going on this weekend – it should show on the list until after midnight on Sunday, since the event goes through Sunday, but since it started on Friday, it already dropped off the list on Friday morning.

    What am I doing wrong?

Viewing 15 replies - 1 through 15 (of 19 total)
  • I think the problem is that your event dates are stored as Y-m-d without a time. strtotime will convert these as if they are ‘Y-m-d 00:00:00’ (zero hour, zero minute, zero seconds), whereas time() provides the actual hour, minute, and second.

    So, if an event date is ‘2014-07-13’, strtotime will give 1405209600. Converting that back to a string will give ‘2014-07-13 00:00:00’.

    Any actual time on that date will have a greater value. To have the compare work the way you want, try adding ‘ 23:59:59’ to the end of the custom meta date, like this:

    $exp_date = strtotime(get_post_meta($post->ID,'event_date_end',true) . ' 23:59:59');
    Thread Starter TrishaM

    (@trisham)

    @vtxyzzy – many thanks for the great explanation. I do think your solution *should* work, but doesn’t……I think that’s because the dates are actually stored as yymmdd (e.g. 20140713 for July 13th, 2014, not as 2014-07-13)

    I’m using Advanced Custom Fields plugin’s datepicker field on this custom post type, so as to make it easy for whomever is entering events – they just click on the start and end dates on a calendar. In the ACF settings it says to store this field as yymmdd. I did play around with other jQuery formats but had numerous problems then getting a query to return any results at all or to sort them in the correct date order.

    So I’ll test out using a Y-m-d format to see if that makes a difference and will report back my results tomorrow.

    The ‘yymmdd’ format should work the same as ‘yyyy-mm-dd’, so there must be another problem. I’ll take another look.

    Did you add the ‘ 23:59:59’ to both lines that use strtotime?

    Another possibility; the time() function returns GMT time, not local time. Try replacing time() with current_time(‘timestamp’).

    One more thing, be sure that the Timezone is set correctly in Admin->Settings->General.

    Thread Starter TrishaM

    (@trisham)

    YAAY! Changing time(); to current_time(‘timestamp’); did the trick.

    Also, I had to move my query above the variables – not sure why, it worked fine before below the query, but after changing the comparison it was not picking up some custom field data for each post, which moving the variables solved.

    SO in case this will help anyone else, here is my working code (I’m including the HTML of what I do with all the custom field data as an example for someone learning how to display future events):

    <?php global $post;
    
    $myposts = get_posts('post_type=event&meta_key=event_date_start&orderby=meta_value_num&order=ASC&posts_per_page=-1');
    foreach ( $myposts as $post ) : setup_postdata( $post );
    
    $eventdate = get_post_meta($post->ID,'event_date_start',true);
    $eventdateend = get_post_meta($post->ID,'event_date_end',true);
    $eventlocation = get_post_meta($post->ID,'event_location',true);
    $eventaddress = get_post_meta($post->ID,'event_location_address',true);
    $eventdirections = get_post_meta($post->ID,'event_location_directions',true);
    $eventcontact = get_post_meta($post->ID,'event_contact',true);
    $eventcontactph = get_post_meta($post->ID,'event_contact_phone',true);
    $eventcontactem = get_post_meta($post->ID,'event_contact_email',true);
    $eventnumdays = get_post_meta($post->ID,'event_number_of_days',true);
    
    $today = current_time('timestamp');
       if ( $eventnumdays == "Multiple Days" ) {
              $exp_date = strtotime(get_post_meta($post->ID,'event_date_end',true) . ' 23:59:59');
       } else {
              $exp_date = strtotime(get_post_meta($post->ID,'event_date_start',true) . ' 23:59:59');
       }
    if ($today <= $exp_date) {
    
    $eventdatestart = strtotime(get_post_meta($post->ID,'event_date_start',true));
    ?>	
    
    <div class="evententry">
    
    <div class="time">
    // Note: separating each date component lets me style the date so it looks like a calendar page //
      <div class="month"><?php echo date('M', $eventdatestart); ?></div>
      <div class="wkday"><?php echo date('l', $eventdatestart); ?></div>
      <div class="day"><?php echo date('d', $eventdatestart); ?></div>
      <div class="year"><?php echo date('Y', $eventdatestart); ?></div>
    </div>
    
    <div class="eventdata">
    <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
    <div class="eventmeta">
    <div class="left">
    <span>Contact:</span><?php echo $eventcontact; ?><br />
    <span>Method:</span><a href="mailto:<?php echo antispambot($eventcontactem); ?>"><?php echo antispambot($eventcontactem); ?></a> &nbsp;&nbsp; <?php echo $eventcontactph; ?>
    </div>
    </div>
    <?php the_excerpt(); ?>
    </div>
    </div><!-- end .evententry -->
    <hr>
    <?php } endforeach; wp_reset_postdata();?>

    HUGE thanks again, @vtxyxxy for all your excellent help!!

    This is brilliant and just what I was looking for!

    Is there any way of adding the Month for events i.e.

    May:
    Event 1
    Event 2

    June:
    Event 3
    Event 4

    Or does that get really complicated?!

    Try changing this:

    $eventdatestart = strtotime(get_post_meta($post->ID,'event_date_start',true));
    ?>

    to this:

    $eventdatestart = strtotime(get_post_meta($post->ID,'event_date_start',true));
          $startmonth = date('M', $eventdatestart);
          if ( $startmonth != $currmonth ) {
             $currmonth = $startmonth; ?>
             <div class="monthheader"><?php echo $startmonth; ?></div>
          <?php }
    ?>

    Amazing! Thanks for this. Works perfectly 🙂

    What I have just noticed with this is if I have

    'posts_per_page' => '-1'

    Everything works correctly and all events show. However if I use

    'posts_per_page' => '8'

    i.e. to limit the amount of events I’d like to show then this doesn’t show any events.

    Any ideas?

    The posts_per_page value should not be in quotes.

    Thanks @vtxyzzy but that makes no difference.

    It might help if your complete code could be seen. Please put the code in a pastebin, and post a link to it here.

    Sorry for the delay in replying. Here’s the code with the infinite amount of posts showing (-1) but if I change that to 8, for example, then no posts show.

    <div class="related-posts p-list cf">
    
    <?php
    // Get the posts
    $myposts = get_posts( array(
    	'post_type' => 'event',
    	'meta_key' => 'event_date_start',
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    	'posts_per_page' => '-1',
    	'post__not_in' => array($post->ID),
    ));
     ?>
    <?php foreach ( $myposts as $post ) : setup_postdata( $post );
    
    $eventdate = get_post_meta($post->ID,'event_date_start',true);
    $eventdateend = get_post_meta($post->ID,'event_date_end',true);
    $eventnumdays = get_post_meta($post->ID,'event_number_of_days',true);
    
    $today = current_time('timestamp');
       if( get_field('event_number_of_days') ) {
              $exp_date = strtotime(get_post_meta($post->ID,'event_date_end',true) . ' 23:59:59');
       } else {
              $exp_date = strtotime(get_post_meta($post->ID,'event_date_start',true) . ' 23:59:59');
       }
    if ($today <= $exp_date) {
    
    $eventdatestart = strtotime(get_post_meta($post->ID,'event_date_start',true));
          $startdate = date('d', $eventdatestart);
          $startmonth = date('F', $eventdatestart);
          $startyear = date('Y', $eventdatestart);
    ?>
    <div class="block">
    <a href="<?php the_permalink() ?>" title="<?php the_title() ?>">
    <div class="image">
    <div class="meta-date">
    <div class="date"><?php echo $startdate; ?> <?php echo $startmonth; ?></div>
    </div>
    <?php the_post_thumbnail('medium', array('class' => 'medium')); ?>
    </div>
    <div class="details">
    <h2 class="title"><?php the_title() ?></h2>
    <?php the_excerpt(); ?>
    </div>
    </a>
    </div>
    <?php } endforeach; wp_reset_postdata();?>
    <?php wp_reset_query(); ?>
    
    </div>
Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Query using custom field for event date not working’ is closed to new replies.