1

I have a second loop on a page where I want to display posts from the same category as the present post but excluding the present post.

I have a loop that displays all the posts from the same category as the current post, but does not exclude the current post.

  <?php
  $project_category = wp_get_post_categories($post->ID); 
  $postid = $post->ID;
  ?>

  <?php
  $the_query = new WP_Query( array(
      'category__in' => $project_category,
      'posts_per_page' => -1,
      'post__not_in' => $postid,
  ) );
  ?>

  <?php                                                      
  $loop = new WP_Query( $the_query );
  while ( $loop->have_posts() ) : $loop->the_post();  
  ?>
1

As explained in Codex

post__not_in (array) - use post ids. Specify post NOT to retrieve.

post__not_in argument have to be passed as array.

Change your query to:

$the_query = new WP_Query( array(
    'category__in'   => $project_category,
    'posts_per_page' => -1,
    'post__not_in'   => array( $postid ),
) );

And it should work.

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.