Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
I don't know if it's related, but it seems like the caller_get_posts argument is deprecated: codex.wordpress.org/Class_Reference/… –  Cyclone Dec 6 '12 at 18:59
1  
Posting self-answered questions that describe a solution to a problem or issue is great, so thanks for that! Unfortunately, it is difficult to tell from your question exactly what the issue/problem being solved is. Can you rewrite your question to describe the issue/problem better? –  Chip Bennett Dec 7 '12 at 1:25

1 Answer 1

up vote 2 down vote accepted

okay now i found the code that makes it able to use a custom taxonomy to show related posts for a custom post type.

  1. $terms = get_the_terms( $post->ID , 'product_tags', 'string'); is the custom taxonomy in which you should put to query all the tags in your custom post type
  2. 'post_type' => 'products' is the custom post type in which calls all the custom tags created in the custom taxonomy in which in this case is product_tags

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.

//Get array of terms
$terms = get_the_terms( $post->ID , 'product_tags', 'string');
//Pluck out the IDs to get an array of IDS
$term_ids = wp_list_pluck($terms,'term_id');

//Query posts with tax_query. Choose in 'IN' if want to query posts with any of the terms
//Chose 'AND' if you want to query for posts with all terms
  $second_query = new WP_Query( array(
      'post_type' => 'products',
      'tax_query' => array(
                    array(
                        'taxonomy' => 'product_tags',
                        'field' => 'id',
                        'terms' => $term_ids,
                        'operator'=> 'IN' //Or 'AND' or 'NOT IN'
                     )),
      'posts_per_page' => 3,
      'ignore_sticky_posts' => 1,
      'orderby' => 'rand',
      'post__not_in'=>array($post->ID)
   ) );

//Loop through posts and display...
    if($second_query->have_posts()) {
     while ($second_query->have_posts() ) : $second_query->the_post(); ?>
      <div class="single_related">
           <?php if (has_post_thumbnail()) { ?>
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail( 'related_sm', array('alt' => get_the_title()) ); ?> </a>
            <?php } else { ?>
                 <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            <?php } ?>
       </div>
   <?php endwhile; wp_reset_query();
   }

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.