I have a custom taxonomy called portfolio-type. The terms = post_type is portfolio and category is portfolio-type

I'm trying to fetch related post of the same category of current post

global $post;
$terms = get_the_terms( $post_id, 'portfolio-type' );
$terms = $terms[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'post_type' =>'portfolio', 'category__in' => wp_get_post_categories( $post_id, 'portfolio-type' ), 'post__not_in' => array($post->ID), 'post_status'=>'publish', 'order'=>'ASC' ));
foreach($myposts as $post) :

<?php endforeach; ?>
<?php wp_reset_query(); ?>

The script shows related posts however doesn't show current post. I'm using this code for showing related posts as a sidebar menu.

  • are we to assume the code here doesn't work? you haven't made it clear exactly what you're asking. do you have debugging enabled?
    – Milo
    Dec 1, 2014 at 0:07
  • @Milo sorry if I didn't explain well. The code is working fine but it doesn't show current post. Let say you have Post1 Post2 Post3 of Category1. And you are at Post1 page the code will show you Post2 and Post3 without showing Post1 which is the current one.
    – Zaher
    Dec 1, 2014 at 0:15

1 Answer 1


Your query explicitly excludes the current post with this:

'post__not_in' => array($post->ID),

remove that and it will return the current post within the results.

  • thank you it worked showing current post however how can I add class="current" to the current post. I found a bug in the script things got messed up as it's not showing posts of only category it's showing random posts of different categories any advice? I'm so appreciable for your support, thank you so much!
    – Zaher
    Dec 1, 2014 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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