Related Posts from one category (4 posts)

  1. zefdesign
    Posted 7 years ago #

    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.

  2. mores
    Posted 7 years ago #

    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.

  3. mores
    Posted 7 years ago #

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

  4. mores
    Posted 7 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic