Support » Fixing WordPress » Related Posts from one category

  • Hey, I have a php code from wp recipes that displays 5 related posts if placed within the loop:

    //for use in the loop, list 5 post titles related to first tag on current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      echo 'Related Posts';
      $first_tag = $tags[0]->term_id;
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

    I would love to be able to make this code only display related posts IF they are from a specific category. Is this doable?

    Thanks so much.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I second this question 🙂

    However, since tags and categories are basically the same thing, couldn’t we use the category-id to check that the post is not part of that ID ?

    let me try that.

    Hmm, how about
    'category__in' => array(5),

    Nope, won’t work in combination with tag__in … so it’s either a tag-list or a category-list …. dang.

    I shall keep searching!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Related Posts from one category’ is closed to new replies.