Related post by category: {if id = to current page id} should not display in related post

I workout wordpress related post by category.
my problem is I don’t want to display the item in current post in the related post.

for example: i have list of item belong to color category which is red, blue, green, black and red is my current page, I don’t want red appear in Related post

<?php foreach((get_the_category()) as $category) {$id = $category->cat_ID;} ?>

     <ul class="recentWorks">
             <?php
            if (have_posts()) : {
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            query_posts("&cat={echo $id;}&showposts=6&paged=$paged&order=ASC");
            }
            while(have_posts()) : the_post();
          ?>
          <li>
            <a class="imageHover" href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a>
            <?php if ( has_post_thumbnail()) the_post_thumbnail('post-thumbnails'); ?>
            <span><?php the_title(); ?></span>
          </li>
          <?php endwhile; ?>
         <?php endif; ?>
          </ul>

Related posts

Leave a Reply

2 comments

  1. You can use arguement :-

    'post__not_in' => array (get_the_ID())
    

    which will exclude the current post from the list
    Like this you can use Wp_Query()

    $args = array(
        'cat' => '{echo $id;}',
        'post__not_in' => array (get_the_ID())
    );
    
    $query1 = new WP_Query( $args );
    
    // The Loop
    while ( $query1->have_posts() ) {
        $query1->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }