Grow your CSS skills. Land your dream job.

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

  • # June 1, 2012 at 1:48 pm

    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();?>

    < ?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>


    < ?php if( get_post_meta($post->ID, "postimg", true) ): ?>

    < ?php else: ?>
    < ?php endif; ?>

    < ?php if( get_post_meta($post->ID, "event_date", true) ): ?>
    < ?php echo get_post_meta($post->ID, "event_date", $single = true); ?>
    < ?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; ?>

    < ?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,40); ?>



    < ?php include (TEMPLATEPATH . '/inc/fullstory_home.php' ); ?>


    < ?php if (($loop->current_post + 1) < ($loop->post_count)) {echo '
    ';}?>

    < ?php endwhile; ?>
    # June 1, 2012 at 2:23 pm

    This is what I use:

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

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

    # June 1, 2012 at 4:39 pm

    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.

    # June 1, 2012 at 5:15 pm

    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
    ";
    $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();?>


    < ?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>


    < ?php if( get_post_meta($post->ID, "postimg", true) ): ?>

    < ?php else: ?>
    < ?php endif; ?>

    < ?php if( get_post_meta($post->ID, "event_date", true) ): ?>
    < ?php echo get_post_meta($post->ID, "event_date", $single = true); ?>
    < ?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; ?>

    < ?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,40); ?>



    < ?php include (TEMPLATEPATH . '/inc/fullstory_home.php' ); ?>


    < ?php if (($loop->current_post + 1) < ($loop->post_count)) {echo '
    ';}?>
    < ?php endwhile; ?>
    # June 4, 2012 at 9:07 am

    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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".