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