display posts with same taxonomy term


I’m trying to display 5 related posts in the loop, those related posts being posts that share the same taxonomy value. e.g. I have a custom taxonomy set up called venues thus each post has a venue taxonomy value assigned to it, so in each post I want to display 5 others posts that share the same taxonomy value (i.e. are at the same venue).
The code I have so far doesn’t work quite right:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
$custom_terms = get_terms('venues');

foreach($custom_terms as $custom_term) {
    $args = array('post_type' => 'listings',
    'posts_per_page' => 5,
        'tax_query' => array(
                'taxonomy' => 'venues',
                'field' => 'slug',
                'terms' => $custom_term->slug,

     $loop = new WP_Query($args);
     if($loop->have_posts()) {

        while($loop->have_posts()) : $loop->the_post(); ?>
        <div class="listing-title"><?php the_title(); ?></div>     
        <?php endwhile;
<?php wp_reset_query(); ?>
<?php endwhile; endif; ?>

It is successfully showing 5 posts, but they are just five posts from the same post type and not 5 posts that share the same taxonomy value in the loop. Any suggestions would be greatly appreciated!

, , , user1374796 3 years 2020-03-31T12:51:37-05:00 0 Answers 109 views 0

Leave an answer