WordPress.org

Ready to get started?Download WordPress

Forums

Exclude Current Post from Query (4 posts)

  1. akabin
    Member
    Posted 2 years ago #

    I have a loop at the bottom of the page that cycles through randomly grabbing more posts from that category and displaying them as thumbnails. I'm able to get it work fine, but I can't figure out how to exclude the current post that is being displayed. Here's my code


    <?php
    global $wp_query;
    $cat_ID = get_the_category($post->ID);
    $cat_ID = $cat_ID[0]->cat_ID;
    query_posts('cat='.$cat_ID.'&showposts=14&orderby=rand');
    ?>
    <?php while (have_posts()) : the_post(); ?>
    <div id="categoriesThumb" onclick="location.href='<?php the_permalink(); ?>';" style="cursor: pointer;">
    <?php if ( has_post_thumbnail()) : ?>
    " title="<?php the_title_attribute(); ?>" >
    <?php the_post_thumbnail(array(85,58)); ?>

    <?php endif; ?>

    ">
    <?php the_title(); ?>
    </div>
    <?php endwhile; ?>

    I've tried various ways to get it to exclude that post, but I can't seem to figure it out. Any suggestions?

  2. Alex
    Member
    Posted 2 years ago #

    did you try using offset? query_posts('cat='.$cat_ID.'&showposts=14&orderby=rand&offset=-1');

  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try:

    <?php
    global $wp_query;
    $cat_ID = get_the_category($post->ID);
    $cat_ID = $cat_ID[0]->cat_ID;
    $this_post = $post->ID;
    query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
    ?>

    http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

  4. akabin
    Member
    Posted 2 years ago #

    Yep, that did the trick! Thanks so much. I had actually tried "post__not_in", but instead of passing $post->ID as a variable, I used it straight instead so it didn't work. Appreciate the help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags