html - WordPress/PHP Looping over a post with a custom taxonomy

one text

I'm creating a page that follows the following format/pattern:

Taxonomy Term Name

Post that belongs to said Taxonomy Term

Taxonomy Term Name

Post that belongs to said Taxonomy Term

Currently, I'm looping over all the taxonomy terms to display them, and then looping over all the posts to display the posts - but I'm not sure how to get this to work properly. Right now, it's somewhat working but actually under every taxonomy term, ALL the posts are being displayed, instead of the ones that specifically belong to that term. Here's my code:

The first part starts the foreach loop of the taxonomy terms ("themes"), the second wp_query part loops through the posts:

<?php foreach ($themes as $theme) { ?>
<h2 class="products-title">
    <?= esc_html($theme->name) ?>
</h2>

    <div>
        <?php foreach ($wp_query->get_posts() as $video) { ?>
            <?php while (have_rows('video', $video->ID)): the_row(); ?>
                <div>
                    <img src="<?= the_sub_field('video_thumbnail') ?>"/>
                    <a href="/video-posts/<?= esc_attr(get_permalink($video->ID)) ?>"><h2><?= the_sub_field('video_title') ?></h2></a>
                </div>
                <hr>
            <?php endwhile; ?>
        <?php } ?>
    </div>

<?php } ?>

Does anyone know how to get this to work?

Source