Display link to taxonomy archive only if it has posts with certain custom field values

There are two solutions that are a little bit smarter, that I can think of…

1. Don’t retrieve everything

You don’t need all info about these posts. All you need is the count, so… You don’t have to get all fields for these posts and you don’t need to get them all…

A “smarter” way to optimize this code is to query only for IDs of posts matching your query and to limit the query to only one result…

<?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 ),
        ),
        'posts_per_page' => 1,
        'fields' => 'ids'
    ) );
    if ( $query->found_posts > 0 ) {
        echo '<li><a href="https://wordpress.stackexchange.com/genre/" . $term->slug . '">' . $term->name . '</a></li>';
    }
}
?>

2. And if that’s not optimized enough…

You can always write custom SQL query that will get only relevant terms… But it’s always a risky way, because such queries will bypass filters… So if there is a filter that is hiding a term or some posts, then your custom query will ignore it.

PS.

This is not the best way to print term link:

<a href="https://wordpress.stackexchange.com/genre/" . $term->slug . '">

You should use get_term_link instead:

<a href="'. esc_attr( get_term_link( $term ) ) .'">