0

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?

  • terms expects an integer, string, or array, you're passing an object. – Milo Mar 26 '18 at 16:10
  • @Milo Any idea what value would need to be passed? I guess I need find the ID of the term? – abbas_arezoo Mar 26 '18 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.