WordPress, related posts, get one of the post’s categories

I am using this code to display related posts:

Read More
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
?><h2> More from this category:</h2> <ul class="related-posts"> <?php
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?> 
        <li class="pov">
            <div class="ex-s1">
            <a href="<?php the_permalink();?>">
    <?php the_post_thumbnail(array(356, 220)); ?>
    </a> 
                </div>
            <div class="ex-n1">
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </div>
            <div class="ex-t1">
           <?php echo get_excerpt(250);?>
              </div>
        </li>

<?php }
wp_reset_postdata(); ?>
  </ul>

And it works with a little problem. Lets say we have post1 and post2. They belong to the category1 and category2. How can I get just posts from category1 and not the posts from cat2 on the single page that displays related posts of the currently viewed post1?

Thx!

Related posts

1 comment

  1. Just take the first item that is returned by the wp_get_post_categories() function

    $cat = wp_get_post_categories($post->ID);
    $related = get_posts( array( 'category__in' => $cat[0], 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
    

Comments are closed.