Support

Account

Home Forums General Issues Separating future/past dates in two queries

Solving

Separating future/past dates in two queries

  • I have a custom field for events for their start date (‘even_date_start’) and want to display on an archive page a list of all the events separated into upcoming events (sorted ASC) and past events (sorted DESC). I have tried to create two queries, but I have only had success in getting the upcoming section to work as desired. With the code shared below the past events go to the ‘else’ fallback which displays a message and no events. I’m sure I’m missing something simple, but I am stumped as to what it is. I’ve tried several things and am sharing my code in it’s current state. Any suggestions or keen eyes welcome!

    `
    <?php
    /**
    */
    get_header();
    ?>

    <section id=”primary” class=”content-area”>
    <main id=”main” class=”site-main”>
    <div id=”post-wrap”>

    <h2>Upcoming Events</h2>

    <?php

    // get today’s date
    $date_now = date( ‘Y-m-d’ );

    // get posts
    $posts = get_posts(array(
    ‘post_type’ => ‘events’,
    ‘posts_per_page’ => -1,
    ‘meta_key’ => ‘event_date_start’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘ASC’,
    ‘compare’ => ‘>=’,
    ‘value’ => $date_now,
    ‘type’ => ‘DATETIME’,
    ));

    if( $posts ): ?>

    <ul class=”events-list”>

    <?php foreach( $posts as $post ):

    setup_postdata( $post )

    ?>
    <li>
    <div class=”entry-content”>
    <?php the_field( ‘event_date_start’ ); ?>
    <?php if( get_field( ‘event_date_end’ ) ):
    echo ” – “;
    the_field( ‘event_date_end’ );
    endif; ?>
    <br/>
    <span class=”event-title”><?php the_title(); ?></span>
    <br />
    <?php the_field( ‘event_location’ ) ?>
    </div>
    </li>

    <?php endforeach; ?>

    </ul>

    <?php wp_reset_postdata(); ?>

    <? else: ?>

    <p><?php _e( ‘No upcoming events are currently on the calendar.’ ); ?></p>

    <?php endif; ?>

    <hr />

    <h2>Past Events</h2>

    <?php

    // get today’s date
    $date_now = date( ‘Y-m-d’ );

    // get posts
    $past_args = array(
    ‘post_type’ => ‘events’,
    ‘posts_per_page’ => -1,
    ‘meta_key’ => ‘event_date_end’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘DESC’,
    ‘compare’ => ‘<‘,
    ‘value’ => $date_now,
    ‘type’ => ‘DATETIME’,
    );

    $query_past = new WP_Query( $past_args );

    if( $query_past->$posts ): ?>

    <ul class=”events-list”>

    <?php foreach( $query_past->$posts as $query_past->$post ):

    setup_postdata( $query_past->$post )

    ?>
    <li>
    <div class=”entry-content”>
    <?php the_field( ‘event_date_start’ ); ?>
    <?php if( get_field( ‘event_date_end’ ) ):
    echo ” – “;
    the_field( ‘event_date_end’ );
    endif; ?>
    <br/>
    <span class=”event-title”><?php the_title(); ?></span>
    <br />
    <?php the_field( ‘event_location’ ) ?>
    </div>
    </li>

    <?php endforeach; ?>

    </ul>

    <?php wp_reset_postdata(); ?>

    <? else: ?>

    <p><?php _e( ‘No past events are currently on the calendar.’ ); ?></p>

    <?php endif; ?>

    </div>

    </main><!– #main –>
    </section><!– #primary –>

    <?php get_footer(); ?>
    `

  • What type of fields are your date fields? Date or Date/Time?

  • They are Date Picker fields.

  • 
    $date_now = date('Ymd');
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Separating future/past dates in two queries’ is closed to new replies.