treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Events custom post type querying by start and end dates (wordpress)

  • Hi, I have a custom post type of 'Events' that have a custom fields for start date (startdate) and end date (enddate). I want to query them so that once an event happens it will it will disappear from the query. I'm not totally sure where to go with this, but any help would be appreciated. Thanks in advance.

    This is the code that I have so far:
    <?php $args = array( 'post_type' => 'events', 'posts_per_page' => 10 ); ?>
    <?php $loop = new WP_Query( $args ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
    <div class="post-entry"><!--post-entry-->
    <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

    <!--Run Timthumb Image Resize Script -> post-img to 200x200px - Zoom Crop of 0 - Quality 75% -->
    <?php if( get_post_meta($post->ID, "postimg", true) ): ?>
    <div class="loop-thumb">
    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/js/timthumb.php?src=<?php echo get_post_meta($post->ID, "postimg", $single = true); ?>&w=120&h120zc=1&q=75" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" style="border:none;" /></a>
    </div>
    <?php else: ?>
    <?php endif; ?>
    <div style="margin-top:5px;">
    <?php if( get_post_meta($post->ID, "event_date", true) ): ?>
    <Strong><?php echo get_post_meta($post->ID, "event_date", $single = true); ?></strong>
    <?php echo date("F d, Y, g:i a",strtotime(get_post_meta($post->ID, 'startdate', true)));?> -
    <?php echo date("F d, Y, g:i a",strtotime(get_post_meta($post->ID, 'enddate', true)));?>
    <?php if( get_post_meta($post->ID, "event_location", true) ): ?>,
    <?php echo get_post_meta($post->ID, "event_location", $single = true); ?>
    <?php else: ?>
    <?php endif; ?>
    <?php else: ?>
    <?php endif; ?>

    <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,40); ?></p>
    <!--Full Story Link, Comments Count, Post Type -->
    <?php include (TEMPLATEPATH . '/inc/fullstory_home.php' ); ?>
    </div>
    </div><!--post-entry-->
    <?php if (($loop->current_post + 1) < ($loop->post_count)) {echo '<div class="post-item-divider"></div>';}?>

    <?php endwhile; ?>
  • This is what I use:

    $CustomValueVariable = get_post_custom_values('CustomValueVariable ', $post->ID);

    <?php echo $CustomValueVariable [0]; ?>

  • I'm afraid I'm not following. What I want to happen is, when an event ends I want it to be removed from the events query.
  • I've been reading some forums and I think I got a little further, but haven't had any luck

    Here's the new code:
    <?php $today = date('Y/m/d h:i A'); //echo "$today<br />";
    $recent = array( 'post_type' => 'events', 'posts_per_page' => 10 );
    $future = recent.'&order=ASC&orderby=meta_value&meta_key=startdate&meta_compare=>=&meta_value='.$today;
    query_posts( $future );
    if (!have_posts())
    query_posts( $recent );

    while ( have_posts() ) : the_post();?>

    <div class="post-entry"><!--post-entry-->
    <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

    <!--Run Timthumb Image Resize Script -> post-img to 200x200px - Zoom Crop of 0 - Quality 75% -->
    <?php if( get_post_meta($post->ID, "postimg", true) ): ?>
    <div class="loop-thumb">
    <a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php bloginfo('template_directory'); ?>/js/timthumb.php?src=<?php echo get_post_meta($post->ID, "postimg", $single = true); ?>&w=120&h120zc=1&q=75" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" style="border:none;" /></a>
    </div>
    <?php else: ?>
    <?php endif; ?>
    <div style="margin-top:5px;">
    <?php if( get_post_meta($post->ID, "event_date", true) ): ?>
    <Strong><?php echo get_post_meta($post->ID, "event_date", $single = true); ?></strong>
    <?php echo date("F d, Y, g:i a",strtotime(get_post_meta($post->ID, 'startdate', true)));?> -
    <?php echo date("F d, Y, g:i a",strtotime(get_post_meta($post->ID, 'enddate', true)));?>
    <?php if( get_post_meta($post->ID, "event_location", true) ): ?>,
    <?php echo get_post_meta($post->ID, "event_location", $single = true); ?>
    <?php else: ?>
    <?php endif; ?>
    <?php else: ?>
    <?php endif; ?>

    <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,40); ?></p>
    <!--Full Story Link, Comments Count, Post Type -->
    <?php include (TEMPLATEPATH . '/inc/fullstory_home.php' ); ?>
    </div>
    </div><!--post-entry-->
    <?php if (($loop->current_post + 1) < ($loop->post_count)) {echo '<div class="post-item-divider"></div>';}?>
    <?php endwhile; ?>
  • It sounds like all you need to do then is grab out the end date into a variable and then wrap your output code in an if statement so that it only outputs the events if the end date is less than the current date.

    This website explains comparing dates:
    http://www.highlystructured.com/comparing_dates_php.html

    So you would have something loosely to the effect of:
    WP_Query
    Loop Posts
    $EndDate = custom meta end date
    if (currrent date is less than $EndDate):

    ---- Spit out the post data ----

    end if
    End Loop Posts