Help required: Displaying related posts within a custom post type single.php template

Hi Guys,

I'm trying to display a list of 5 random 'related' posts based on the custom taxonomy of the post that's being viewed.

For example, when a visitor is viewing an advice post (custom post type 'advice') that has been assigned to the 'sponsorship' taxonomy (custom taxonomy slug 'advice_category/sponsorship'), I want to output 5 random 'sponsorship' posts in an unordered list.

The code needs to sit within the loop of the single-advice.php template.

Any help would be hugely appreciated!

Thanks in advance!

  • DavidM

    Hello Julian,

    I believe the following should do! Though the developers around here might be able to provide a cleaner solution for the counter. I tested it using the Directory plugin and its directory_listing custom post type along with one my taxonomies and it worked just dandy!

    <?php
    $post_type = 'advice';
    $tax = 'sponsorship';
    $post_count = 5;
    $counter = 0;
    $tax_terms = get_terms($tax);
    echo 'Related Advice';
    if ($tax_terms) {
    	foreach ($tax_terms as $tax_term) {
    	$args = array(
    		'post_type' => $post_type,
    		"$tax" => $tax_term->slug,
    		'post_status' => 'publish',
    		'orderby' => rand
    	);
    
    	$my_query = null;
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    		echo '<ul class="sponsorship-post-list">';
    		while ($my_query->have_posts() && $counter < $post_count) : $my_query->the_post(); ?>
    		<li>
    			<h3 class="sponsorship-post"><?php the_title(); ?></h3>
    			<?php the_excerpt(); ?>
    		</li>
    		<?php
    		$counter += 1;
    		endwhile;
    		echo '</ul>';
        }
        wp_reset_query();
    	}
    }
    ?>

    Let me know how that works for you! :slight_smile:

    Cheers,
    David

  • Julian Evans

    I've encountered one small problem, possibly a silly mistake on my part.

    The following code:

    <?php
    		$post_type = 'advice';
    		$tax = 'sponsorship';
    		$post_count = 5;
    		$counter = 0;
    		$tax_terms = get_terms($tax);
    		if ($tax_terms) {
    			foreach ($tax_terms as $tax_term) {
    			$args = array(
    				'post_type' => $post_type,
    				"$tax" => $tax_term->slug,
    				'post_status' => 'publish',
    				'orderby' => rand
    			);
    			$my_query = null;
    			$my_query = new WP_Query($args);
    			if( $my_query->have_posts() ) {
    				echo '<ul id="related-posts" class="item-list">';
    				echo '<h6>Related Posts</h6>';
    				while ($my_query->have_posts() && $counter < $post_count) : $my_query->the_post(); ?>
    				<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    				<?php
    				$counter += 1;
    				endwhile;
    				echo '</ul>';
    			 }
    			 wp_reset_query();
    			}
    		}
    		?>

    Is outputting a surplus list:

    <ul id="related-posts" class="item-list">
    	<h6>Related Posts</h6>
    	<li><a href="#" title="Make Your Club Sponsor Friendly">Make Your Club Sponsor Friendly</a></li>
    	<li><a href="#" title="7 Steps to Effective Marketing for Sports Clubs">7 Steps to Effective Marketing for Sports Clubs</a></li>
    	<li><a href="#" title="Targeting Potential Sponsors">Targeting Potential Sponsors</a></li>
    	<li><a href="#" title="How to Write a Solid Marketing Plan">How to Write a Solid Marketing Plan</a></li>
    	<li><a href="#" title="Working With Your Sponsors">Working With Your Sponsors</a></li>
    </ul>
    <ul id="related-posts" class="item-list">
    	<h6>Related Posts</h6>
    </ul>

    Any ideas?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.