Posts on my site describe future events, with the event date recorded in a custom field in Unix time.
Generally, only upcoming events are relevant to visitors. As such, I'd like to display navigational links to taxonomy archive only if the taxonomy has at least one upcoming event (event date > today's date).
Is there a smarter way than the following?
<?php
$terms = get_terms( array(
'taxonomy' => 'genre',
'hide_empty' => true,
) );
$today = time() - 86400;
foreach($terms as $term) {
$query = new WP_Query( array( 'meta_key' => 'event_date', 'meta_value' => $today, meta_compare => '>=', 'tax_query' => array( array( 'taxonomy' => 'genre', 'field' => 'term_id', 'terms' => $term->term_id, ), ), ) );
if (($query->found_posts) > 1) {
echo '<li><a href="/genre/' . $term->slug . '">' . $term->name . '</a></li>';
}
} ?>
This works as expected, but adds about 100 queries to each page load which is a little disturbing.
found_posts > 1
if you want to show terms that have at least one item? If the term contains only one post, should it be shown or not?