I’m trying to display related posts by a custom taxonomy and only have the solution for related posts using a category.
$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<div id="relatedposts" class="clearfix"><h4>Related Posts</h4><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<?php
if ( has_post_thumbnail() ) { ?>
<li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php echo the_post_thumbnail(); ?></a>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><div class="title"><?php the_title(); ?></div></a>
</div>
</li>
<?php }
?>
<?
}
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query();
The following code only works for post types with registered category not custom taxonomy types.
okay now i found the code that makes it able to use a custom taxonomy to show related posts for a custom post type.
Input this code anywhere inside your loop or query to show all posts within your custom post type. This does not filter your custom post type based on different tags. This shows all the tags within your custom post type, which in this case is products.
okay now this code helps solve the issue with using custom post types to filter in a custom taxonomy. For example, I had project as my taxonomy and for custom post type. When i add different tags to each project they create a tagportfolio taxonomy in which add all the taxonomies and queries them.