WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

OK - Looked everywhere for this one.


Custom Post Type #1 = 'product' > registered taxonomy 'productranges'

Custom Post Type #2 = 'testimonial' > registered taxonomy 'testimonials'

Both CPTs share categories 'Complete Food' , 'Treats', etc.. for example


How can I display a random post excerpt of the same shared-category from a 'Testimonial' post into 'Product' post? In a sense linking the Testimoinals to the Products.

I've used the following but it only shows a random post from all the categories, I need it to output from the current-shared category being viewed:

$posts = get_posts('post_type=testimonial&issue=$issueid&post_status=publish&numberposts=1&orderby=rand');

foreach ($posts as $post) : setup_postdata($post); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt('<div class="readmore">'); ?><a href="/testimonials">read more...</a> 

<?php endforeach; ?>

//Reset Query

Cheers if you can help!

share|improve this question
Have you tried putting your get_posts in an array? – fdsa Jun 27 '12 at 22:05
The problem is I'm not sure how to call the current-page's taxonomy dynamically in order for the one template to serve all products. – Beags Jun 27 '12 at 22:29
Is the taxonomy a category? – fdsa Jun 27 '12 at 23:48
Yes - they are categories. The top Taxonomies registered are 'productranges' and 'testimonials'. – Beags Jun 28 '12 at 8:04
Edited initial post to be more clear... – Beags Jun 28 '12 at 9:29

You need to get_the_category() of the product post and then set category in get_posts() to that category's $category->term_id.

share|improve this answer
Have sorted it (well - kind chap over at Wordpress.org nailed it):link – Beags Jun 29 '12 at 9:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.