WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display random CPT#1 excerpt in single that shares category with CPT#2 (14 posts)

  1. elbego
    Member
    Posted 1 year ago #

    OK - Looked everywhere for this one.

    **Details:**

    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

    **Question:**

    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:

    <?php
    	$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; ?>
    	<?php
    
    	//Reset Query
    	wp_reset_query();?>

    Cheers if you can help!

  2. jnhghy
    Member
    Posted 1 year ago #

    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 :)
    Regards

  3. elbego
    Member
    Posted 1 year ago #

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

    Many thanks for your time.

  4. jnhghy
    Member
    Posted 1 year ago #

    <?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 ยป' );
    endwhile;
    endif;
    ?>

    Regards

  5. elbego
    Member
    Posted 1 year ago #

    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):
    taxonomy=testimonials&tag_ID=4&post_type=testimonial

    category 1: (products):
    taxonomy=productranges&tag_ID=4&post_type=fw_product

    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]

  6. jnhghy
    Member
    Posted 1 year ago #

    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.

    Regards

  7. elbego
    Member
    Posted 1 year ago #

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

  8. jnhghy
    Member
    Posted 1 year ago #

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

    Regards

  9. elbego
    Member
    Posted 1 year ago #

    You are indeed a gent..

    http://pastebin.com/ArsmRW1n

  10. jnhghy
    Member
    Posted 1 year ago #

    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.

    Regards

  11. elbego
    Member
    Posted 1 year ago #

    You've absolutely nailed it!

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

  12. jnhghy
    Member
    Posted 1 year ago #

    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.
    Regards

  13. elbego
    Member
    Posted 1 year ago #

    A pleasure to resolve it - I just need to make sure I learn from this...

    Thanks again

  14. elbego
    Member
    Posted 1 year ago #

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

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic