paullacey78
12/14/2017 - 3:46 PM

Related Posts by Taxonomy

Related Posts by Taxonomy

<?php 
// get the custom post type's taxonomy terms
$custom_taxterms = wp_get_object_terms( $post->ID, 'TAXONOMY_SLUG', array('fields' => 'ids') );

// arguments
$args = array(
'post_type' => 'TAXONOMY_SLUG',
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'rand',
'tax_query' => array(
    array(
        'taxonomy' => 'TAXONOMY_SLUG',
        'field' => 'id',
        'terms' => $custom_taxterms
    )
),
'post__not_in' => array ($post->ID),
);

// Run the query
$related_items = new WP_Query( $args );

// loop through query
if ($related_items->have_posts()) :
	// open wrappers
	echo '<div>';
		while ( $related_items->have_posts() ) : $related_items->the_post();

			// Here is your markup for your single item
			// I have used a pods template below in this case (because trying to learn PODS)
			// You could put your output here instead of a Pods template if you wanted
			echo do_shortcode( '[pods template="Related posts from post term"]' );

		endwhile;
	// close wrappers
	echo '</div>';
endif;

// Reset Post Data
wp_reset_postdata();
<div class="grid-50 tablet-grid-50">	
	<div class="fl-post-image">
		<a href="{@permalink}">{@post_thumbnail.large}</a>
	</div>
	<ul class="colorful_categories">
		[each theme]<li><a href="{@theme.permalink}" style="background:{@theme.color};">{@theme.name}</a></li>[/each]
	</ul>
	<h2 class="fl-post-feed-title"><a href="{@permalink}">{@post_title}</a></h2>
	<p>{@post_content, my_pods_excerpt}</p>
	<div class="fl-post-more-link">
		<a href="{@permalink}">Read</a>
	</div>
</div>