How to simplify my WP_Query(s)?

Can anybody help me to simplify the WP_Query(s) below please? They have the very same structure, but one to target the future, and one for past events.

<?php
    $future = date('Ymd', strtotime("+1 year"));
    $now = date('Ymd', strtotime("now"));
    $past = date('Ymd', strtotime("-1 year"));
?>

<?php
    $args_future = array(
        'post_type' => 'event',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'event_date',
                'value' => array($now, $future),
                'type' => 'numeric',
                'compare' => 'BETWEEN'
            )
        )
    );
    $query_future = new WP_Query($args_future);
?>

<?php
    echo '<h2>Future events</h2><ul class="future-events">';
    while($query_future->have_posts()) {
        $query_future->the_post();
        echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
    } echo '</ul>'; wp_reset_postdata();
?>

<?php
    $args_past = array(
        'post_type' => 'event',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'event_date',
                'value' => array($past, $now),
                'type' => 'numeric',
                'compare' => 'BETWEEN'
            )
        )
    );
    $query_past = new WP_Query($args_past);
?>

<?php
    echo '<h2>Past events</h2><ul class="past-events">';
    while($query_past->have_posts()) {
        $query_past->the_post();
        echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
    } echo '</ul>'; wp_reset_postdata();
?>

Related posts

Leave a Reply

1 comment

  1. Your code isn’t too bad. Splitting it into several loops is pretty much unavoidable in this case if you want to maintain readability. All I did here was organize your code into a multidimensional array. This is untested, but should get you started:

    <?php
    $future = date('Ymd', strtotime("+1 year"));
    $now = date('Ymd', strtotime("now"));
    $past = date('Ymd', strtotime("-1 year"));
    $args = array(
        'future'=>array(
            'ul_class'=>'future-events',
            'label'=>'Future Events',
            'query_args'=>array(
                'post_type' => 'event',
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                        'key' => 'event_date',
                        'value' => array($now, $future),
                        'type' => 'numeric',
                        'compare' => 'BETWEEN'
                    )
                )
            )
        ),
        'past'=>array(
            'ul_class'=>'past-events',
            'label'=>'Past Events',
            'query_args'=>array(
                'post_type' => 'event',
                'posts_per_page' => -1,
                'meta_query' => array(
                    array(
                        'key' => 'event_date',
                        'value' => array($past, $now),
                        'type' => 'numeric',
                        'compare' => 'BETWEEN'
                    )
                )
            )
        )
    );
    foreach($args as $arg){
        $q = new WP_Query($arg['query_args']);
        printf('<h2>%s</h2><ul class="%s">', $arg['label'], $arg['ul_class']);
        while($q->have_posts()) : $q->the_post();
            echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
        endwhile;
        echo '</ul>';
    }
    wp_reset_postdata();
    ?>