2

I'm trying to display related posts by (specific) 'post_tag' taxonomy in custom post type. Following code is displaying all post by a custom post type.

I mean i want to display related posts by post tag of 'Mp3' custom post type in its single post page.

$post_type = 'mp3';
    $tax = 'post_tag';
    $post_count = 5;
    $counter = 0;
    $tax_terms = get_terms($tax);
    if ($tax_terms) {
        foreach ($tax_terms as $tax_term) {
        $args = array(
            'post_type' => $post_type,
            "$tax" => $tax_term->slug,
            'post_status' => 'publish',
            'orderby' => 'rand',
            'showposts' => 5
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
            echo '<ul id="related-posts" class="item-list">';
            echo '<h6>Related Posts</h6>';
            while ($my_query->have_posts() && $counter < $post_count) : $my_query->the_post(); ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
            <?php
            $counter += 1;
            endwhile;
            echo '</ul>';
         }
         wp_reset_query();
        }
    }

So I'm here asking for solution...

0

Try this:

On the single view, you need to use get_the_terms() rather than get_terms(). That way, you're getting the terms that are actually assigned to the post, rather than all the terms that exist.

    $post_type = 'mp3';
    $tax = 'post_tag';
    $post_count = 5;
    $counter = 0;
    $tax_terms = get_the_terms($post->ID, $tax);

    if ($tax_terms) {
      foreach ($tax_terms as $tax_term) {
      $args = array(
        'post_type' => $post_type,
        "$tax" => $tax_term->slug,
        'post_status' => 'publish',
        'orderby' => 'rand',
        'posts_per_page' => 5
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo '<ul id="related-posts" class="item-list">';
        echo '<h6>Related Posts</h6>';
        while ($my_query->have_posts() && $counter < $post_count) : $my_query->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
        <?php
        $counter += 1;
        endwhile;
        echo '</ul>';
       }
       wp_reset_query();
      }
    }

NOTE: This code will produce multiple lists of related posts. If the current post has 2 tags assigned to it, you'll get 2 lists of related posts. If you'd rather nave one list of related posts from both tags combined, you'd use something like this:

$post_type = 'mp3';
$tax = 'post_tag';
$post_count = 5;
$counter = 0;
$tax_terms = get_the_terms($post->ID, $tax);

if ($tax_terms) {
  $tax_term_ids = array();
  foreach ($tax_terms as $tax_term) {
    $tax_term_ids[] = $tax_term->term_id;
  }

  $args = array(
    'post_type' => $post_type,
    "tax_query" => array(
      array(
        'taxonomy' => $tax,
        'terms' => $tax_term_ids,
        )
      ),
    'post_status' => 'publish',
    'orderby' => 'rand',
    'posts_per_page' => 5
  );
  $my_query = null;
  $my_query = new WP_Query($args);
  if( $my_query->have_posts() ) {
    echo '<ul id="related-posts" class="item-list">';
    echo '<h6>Related Posts</h6>';
    while ($my_query->have_posts() && $counter < $post_count) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php
    $counter += 1;
    endwhile;
    echo '</ul>';
   }
   wp_reset_query();

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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