Next two dates from array

My WordPress theme allows for the addition of dates to an ‘events’ custom post type. The stock theme shows all dates entered however I have limited to two dates. Unfortunately it’s the first two dates not the next two dates for the selected event.

So as the year passes by, I’d like php to look at the current date and then display forthcoming events … i.e. the next two event dates from the array.

Read More

Here’s my code:

<div id="event-days">
        <ul>
        <?php
        global $va_locale;
        $days = va_get_the_event_days();

        $i = 0;
        $len = count($days);

        foreach( $days as $date_U => $term ) { ?>
            <?php $date = $term->slug; ?>
            <?php $display_date = $va_locale->date( apply_filters( 'va_single_event_dates_date_format', get_option( 'date_format' ) ), strtotime( $date ) );?>
            <?php $times = va_get_the_event_day_times( $date ); ?>
            <li><?php echo html_link( va_event_day_get_term_link( date( 'Y-m-d', strtotime( $date ) ) ), $display_date ); ?><?php echo va_get_the_event_day_time( $times, ' - ', ' @ ' );
            if ($i == 0) { ?>
                <meta itemprop="startDate" content="<?php echo date( 'c', strtotime( $date ) ); ?>" />
            <?php } ?>
            </li>
        <?php $i++; 
        if($i==2) break;
        } ?>
        </ul>
    </div>

Thanks in advance for your help.

edit – here we go gavriel. cheers

function va_get_the_event_days( $event_id = 0 ) {
    $event_id = $event_id ? $event_id : get_the_ID();

    $terms = get_the_terms( $event_id, VA_EVENT_DAY );
    if ( !$terms )
        return array();

    $days = array();
    foreach ( $terms as $term_id => $term ) {
        if ( $term->parent != 0 ) {
            $days[ strtotime($term->slug) ] = $term;
        }
    }

    return $days;
}

Related posts

1 comment

  1. The easiest change would be to only add the future events to the array:

    $now = time();
    foreach ( $terms as $term_id => $term ) {
        if ( $term->parent != 0 ) {
            $term_timestamp = strtotime($term->slug);
            if ($term_timestamp > $now) {
                $days[ $term_timestamp ] = $term;
            }
        }
    }
    

Comments are closed.