Home » Labs » How to show WordPress related posts from tags  

How to show WordPress related posts from tags


Filed under Labs

Below code will display related posts from tags.It uses the wp_query function to look matches from tags. Style the query according to your needs.

 
	<?php 
		$temp_post = $post;
		global $post;
		$tags = wp_get_post_tags($post->ID);
		if ($tags) {
			$tag_ids = array();
			foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
			$args=array(
			'tag__in' => $tag_ids,
			'post__not_in' => array($post->ID),
			'posts_per_page'=>10, 
			'caller_get_posts'=>1
		);
 
		$new_query = new wp_query( $args );
 
		if( $new_query->have_posts() ) {
		echo '<h4>Related Posts</h4>';
		while( $new_query->have_posts() ) {
		$new_query->the_post(); 
 
	?>
		<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
			<?php the_title(); ?>
		</a>
		 <?php the_time('M j, Y') ?>
 
		 <?php the_excerpt();?>
 
	<? 
		}
		}
		}
		$post = $temp_post;
		wp_reset_query(); 
	?>
It's only fair to share...Share on Google+Tweet about this on TwitterShare on Facebook

Posts you may like: