Support » How-To and Troubleshooting » [Resolved] Display random CPT#1 excerpt in single that shares category with CPT#2

[Resolved] Display random CPT#1 excerpt in single that shares category with CPT#2

  • 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!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Hi I am using query_posts on one of my sites with custom post types so I can show you how you can achieve this with query_posts and you can change it afterwards in get_posts :

    $query_tax = array('taxonomy' => 'testimonials','field' => 'id','terms' => $query_cat);
    $args = array('tax_query'=>array($query_tax),'posts_per_page' => 1,'orderby' => 'rand','post_type' => 'testimonials', 'paged'=>$paged);
    $posts = query_posts ($args);

    where $query_cat is the id of the categori that you want the cpt to be from.
    Hope this helps, if you have issues with the code… we’ll try to fix it ๐Ÿ™‚

    forgive my ignorance but please can you show how you integrate the get_posts output following?

    Many thanks for your time.

    <?php within the loop:

    if (have_posts()) : while (have_posts()) : the_post();
    //here you can display your cpt excrept
    ?><h3><?php the_title(); ?></h3><?php
    // and so on ... make sure you verify the loop variables because some of them are specific for the loop while others might be specific to outside the loop ... hope it will work out of the box
    	the_content( 'Read the full post ยป' );


    Doesn’t seem to work. I thought I’d check that the categories are linked correctly, here are the links to and example category from each CPT:

    category 1 (testimonials):

    category 1: (products):

    Both have ID4 so should work there?

    Could the issue be that it’s on a product page that I’m wanting the output to appear at the end of the main content?

    Page code (your code at bottom:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Hi again, I spotted at least one issue, the $query_cat variable has no value, you need to give the correct category id to this variable, you said you are using same category for both cpt#1 and cpt#2 so in your code, in the first loop, get the category id that the cpt#1 has and save it in $query_cat, this is needed to query the correct/any post.


    You’ll have to forgive me but I have no idea how to do that!

    please use pastebin so I will have access to your code (hole page) and I will give you the code in a few hours.


    You are indeed a gent..


    Hi, sorry for the delay
    I’ve edited the code, it might have some errors but this should give you a hint on what way I would go.
    See here edited rows start from line: 23
    Hope it helps.


    You’ve absolutely nailed it!

    Thanks so much for this – I was at melting point.

    WOW … that’s a touch down :))
    I’m very happy to see it fixed so fast and glad to help, also if the issue is fiexed you can mark this topic as resolved.

    A pleasure to resolve it – I just need to make sure I learn from this…

    Thanks again

    To update – I had to remove

    $le_id = the_ID();

    From the beginning as it was outputting the page ID as text at the top of the page

    It doesn’t seem to matter not using it!?


Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘[Resolved] Display random CPT#1 excerpt in single that shares category with CPT#2’ is closed to new replies.