I have a custom post type for accessories. When you view the post it also shows related posts. It looks great, but it also shows the current post within related posts.

Is there a way to exclude the current post from the loop?

    $category = get_the_category(); 
    $model = $category[1]->cat_name;
    $accessory = array('numberposts' => 8, 'offset'=> 1, 'post_type' => 'accessory', 'category_name' => $model, 'order' => 'DESC');
    query_posts( $accessory );
      <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>
            <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'openeye' ), 'after' => '</div>' ) ); ?>
            <h4><?php the_title(); ?></h4>
            Part Number: <?php echo get_post_meta($post->ID, "accessory-part-number", true); ?>
            <?php $desc = get_post_meta($post->ID, "accessory-description", true); ?>
            <p><?php echo utf8_truncate( $desc ); ?></p>
            <a href="<?php echo get_permalink(); ?>">Learn more about the <?php the_title(); ?></a>
        <?php endwhile; ?>
      <?php endif; ?>

2 Answers 2


Try this:

For your single-accessory.php template:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

      // excludes this post from 'Related posts' in the sidebar
      $GLOBALS['current_id'] = $post->ID; 


For your sidebar or where you want to show related posts:

        if (is_singular('accessory') ) :
        global $post;
        $categories = get_the_category();
            $exclude = $GLOBALS['current_id'];
        $args = array(
        'post_type' => 'accessory',
        'post__not_in' => array($exclude),
        'posts_per_page' => -1

        foreach ($categories as $category) :
        $posts = get_posts($args);

        if(count($posts) > 1) {

                 //do stuff

First, you should never use query_posts() for secondary Loops.

As for excluding current post, it would take something like this in arguments:

'post__not_in` => array( get_the_ID() )
  • Totally make sense, thx! What's odd though is that each returns entirely different results. Is there something else I need to know or do regarding querys? Commented Mar 21, 2011 at 23:13
  • @Zach Shallbetter I am not sure what each and how different, could you please rephrase that or add examples to original question?
    – Rarst
    Commented Mar 21, 2011 at 23:18
  • Sure, WP_Query returns no results. Get_Posts returns one result and of course query_posts returns everything. Commented Mar 21, 2011 at 23:23
  • @Zach Shallbetter I have no idea what to think of that. query_posts() and get_posts() are essentially just calls to WP_Query.
    – Rarst
    Commented Mar 22, 2011 at 8:43
  • Wild, I'm obviously doing something incorrect then. Commented Mar 22, 2011 at 15:46

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.