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.

		$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;
			'tag__in' => $tag_ids,
			'post__not_in' => array($post->ID),
		$new_query = new wp_query( $args );
		if( $new_query->have_posts() ) {
		echo '<h4>Related Posts</h4>';
		while( $new_query->have_posts() ) {
		<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
			<?php the_title(); ?>
		 <?php the_time('M j, Y') ?>
		 <?php the_excerpt();?>
		$post = $temp_post;
It's only fair to share...Share on Google+Tweet about this on TwitterShare on Facebook

Posts you may like: