Display title of all custom posts from same taxonomy term on a custom post template

I have a custom post and I want to display other custom post data from within the same taxonomy term on the template. Ideally i’d like to use a foreach(); loop as this fits in with the other loops within the theme.

I could declare the term within the config below but I’d like it to be a little more flexible as there are a number of terms with the taxonomy.

For example, I have products within a taxomony. On each product template I want to be able to see other products within the same taxonomy.

I’m struggling to output the data. Here’s what I have so far which isn’t giving me what I want:

<?php
$term = $wp_query->queried_object;
$products = get_posts(array(
‘post_type’ => ‘my_custom_post’,
‘posts_per_page’ => -1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘my_taxonomy’,
‘field’ => ‘slug’,
‘terms’ => $term
)
)
));
?>

<?php foreach($products as $product): ?>
<?php echo $product->post_title; ?>
<?php endforeach;?>

Is it possible to do this using the foreach(); loop? And is it then possible to add a class to the ‘current’ custom post type?

Read more here:: Display title of all custom posts from same taxonomy term on a custom post template

Leave a Reply

Your email address will not be published. Required fields are marked *