<?php
//for in the loop, display all "content", regardless of post_type,
//that have the same custom taxonomy (e.g. genre) terms as the current post
//$backup = $post;  // backup the current object
$found_none = '';
$portfolio_column_count=1;
$columns=4;
$taxonomy = 'types';//  e.g. post_tag, category, custom taxonomy
$param_type = 'types'; //  e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$portfolioImage_type="portfolio-small";

$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);

$first_tag="";
$second_tag="";
$third_tag="";
if (isset($tags[0]->term_id)) $first_tag 	= $tags[0]->term_id;
if (isset($tags[1]->term_id)) $second_tag 	= $tags[1]->term_id;
if (isset($tags[2]->term_id)) $third_tag 	= $tags[2]->term_id;
//$pTags = wp_get_post_terms( get_the_ID(), 'tagportfolio' );
//print_r($tags);
?>

<?php
if ($tags) {
	//echo $tag->slug;
	
	foreach($tags as $each_tag) {
        $tag_ids[] = $each_tag->term_id;
 
	    //WP_Query arguments.
	    $tag_args = array(
				'taxonomy' => $taxonomy,
				'terms' => $tag_ids,
				'field' => 'id',
				'operator' => 'IN'
	    );
	}

	$args = array(
		'post_type' => get_post_type($post->ID),
		'post__not_in' => array($portfolio_post_ID),
		'posts_per_page' => 4,
		'tax_query' => array(
			'relation' => 'OR', $tag_args
		)
	);
    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
		?>
		<h3 class="related_posts_title"><?php echo of_get_option('related_ptitle'); ?></h3>

		<div class="portfolio-related-wrap clearfix">
			<ul class="portfolio-four">
		
		<?php
		while ($my_query->have_posts()) : $my_query->the_post();

		if ($portfolio_column_count>$columns) $portfolio_column_count=1;
		$custom = get_post_custom($post->ID);
		
		if ( isset($custom["video"][0]) ) { $video_url=$custom["video"][0]; }
		if ( isset($custom["thumbnail"][0]) ) { $thumbnail=$custom["thumbnail"][0]; }
		if ( isset($custom["custom_link"][0]) ) { $link_url=$custom["custom_link"][0]; }
		$portfolio_thumb_header=$custom["portfolio_page_header"][0];
		
		$protected="";
		$iconclass="column-portfolio-icon";
		if ( post_password_required() ) { $protected=" portfolio-protected"; $iconclass=""; }
		?>
		<li class="portfolio-col-<?php echo $portfolio_column_count++; echo $protected; ?>">
			<?php
			if ( $custom["video"][0]<>"" ) {
				$p_class="fadethumbnail-play";
			} elseif ( $custom["custom_link"][0]<>"" ) {
				$p_class="fadethumbnail-link";
			} else {
				$p_class="fadethumbnail-view";
			}
			?>
				<?php
				// If Password is set
				if ($portfolio_thumb_header !="Slideshow") {
					if ( post_password_required() ) {
						echo '<a class="portfolio-image-link portfolio-columns" title="'.get_the_title().'" href="'.get_permalink().'" >';
						echo '<span class="'.$iconclass.' portfolio-image-icon"></span>';
						} else {
						// For no password protected portfolio items
						if ( $custom["custom_link"][0]<>"" ) {
							echo '<a class="portfolio-image-link portfolio-columns" title="'.get_the_title().'" href="'.$custom["custom_link"][0].'" >';
							echo '<span class="'.$iconclass.' portfolio-link-icon"></span>';
						} else {
							echo '<a class="portfolio-image-link portfolio-columns" title="'.get_the_title().'" href="'.get_permalink().'" >';
							echo '<span class="'.$iconclass.' portfolio-image-icon"></span>';
						}
					}
				}
				?>
				<?php
				// Show Image
				if ( ! post_password_required() ) {
					if ($thumbnail<>"") {
						echo '<img src="'.$thumbnail.'" class="preload-image displayed-image" alt="thumbnail" />';
					} else {
						
						if ($portfolio_thumb_header=="Slideshow") {

							$flexi_slideshow = do_shortcode('[flexislideshow imagesize="'.$portfolioImage_type.'"]');
							echo $flexi_slideshow;
							
						} else {
						
							echo display_post_image (
								$post->ID,
								$have_image_url="",
								$link=false,
								$type=$portfolioImage_type,
								$post_title=get_the_title(),
								$class="preload-image displayed-image"
							);
						}
					}
				}
				?>		
				</a>
				<div class="work-details">
					<h4><a href="<?php if ($link_url<>"") { echo $link_url; } else { the_permalink(); } ?>" rel="bookmark" title="<?php echo get_the_title(); ?>"><?php the_title(); ?></a></h4>
					<p class="entry-content work-description"><?php echo $custom["description"][0];?></p>
				</div>
		</li>

			<?php $found_none = '';
		endwhile;
    }
?>
	</ul>
</div>
<?php
}
wp_reset_query();
?>