Title: Using loops and query_posts
Last modified: August 18, 2016

---

# Using loops and query_posts

 *  [clarke1866](https://wordpress.org/support/users/clarke1866/)
 * (@clarke1866)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/)
 * Greetings,
 * I’m messing around with the loop in WP and I’m trying to get some custom query’s
   working. Here is the basic query:
    ` if (have_posts()) : while (have_posts()):
   the_post(); // Do stuff like format each post etc endwhile;
 * It works just fine. Here is the trick, how can I do the same exact query but 
   instead of ‘doing stuff’ to every post, do stuff to every post except a post 
   whose id is XX (whatever # I choose).
 * **In other words, how can I use query_posts (or any query, I dont’ care which
   function) to get and display all posts except one whose ID I specify?**
 * Is it posible to use something like` !=$some_post_number` where $some_post_number
   is already declared?
 * Thanks for any help.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/using-loops-and-query_posts/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-loops-and-query_posts/page/2/?output_format=md)

 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192286)
 * There’s no way to specify to exclude a post by post ID in The Loop. That said,
   is there any reason you can just skip over it in your Loop processing? In other
   words:
 * `if (have_posts()) : while (have_posts()) : the_post();
    if( $post->ID == $that_post_I_hate)
   continue; // Do stuff like format each post etc endwhile;
 *  Thread Starter [clarke1866](https://wordpress.org/support/users/clarke1866/)
 * (@clarke1866)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192288)
 * I’ll give it a whirl, that sounds like a much better idea than what I had in 
   mind! Will post results
 *  [Cyndy Otty](https://wordpress.org/support/users/ceo/)
 * (@ceo)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192315)
 * If that doesn’t work, you could always create a special category for that post
   you want to exlude and then just exclude that category in the loop. (Or if it’s
   just a post you’re trying to leave out…you could always make it private, unless
   you’re not trying to hide it from the public altogether.)
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192316)
 * It just never makes sense for me why to make a post if you don’t want to show
   it to anyone?
 *  [Root](https://wordpress.org/support/users/root/)
 * (@root)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192366)
 * You need something like:
 * <?php query_posts('p=-1');?>
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192372)
 * > You need something like:
   > `<?php query_posts('p=-1');?>`
 * Which currently _doesn’t_ exist. Just didn’t want to confuse the initial poster.
 *  [Root](https://wordpress.org/support/users/root/)
 * (@root)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192373)
 * What do you mean it doesnt exist? Where does it not exist ?
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192377)
 * I mean there’s no point in the code where WP correctly handles a negative number
   for a ‘p’ in the query string. It simply gets placed in the WHERE clause of the
   query with an ” AND ID = “.
 *  [Root](https://wordpress.org/support/users/root/)
 * (@root)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192379)
 * Is that true for cats as well? or is cat=-2 Ok?
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192380)
 * Yes, `cat` _does_ have special handling for a single exclusion.
 *  [Root](https://wordpress.org/support/users/root/)
 * (@root)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192381)
 * If that is right – and it presumably is because coldforged knows whereof he speaks
   I would allocate the private post to its own cat and exclude that.
 *  [Root](https://wordpress.org/support/users/root/)
 * (@root)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192382)
 * Thanks for that.
 *  [ColdForged](https://wordpress.org/support/users/coldforged/)
 * (@coldforged)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192384)
 * Most welcome.
 *  [ifelse](https://wordpress.org/support/users/ifelse/)
 * (@ifelse)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192388)
 * _Yes, cat does have special handling for a single exclusion._
    … though currently
   imperfect special handling:-)
 *  Thread Starter [clarke1866](https://wordpress.org/support/users/clarke1866/)
 * (@clarke1866)
 * [21 years ago](https://wordpress.org/support/topic/using-loops-and-query_posts/#post-192544)
 * Hey all, I wrote up why I wanted to use two loops and skip a post. Head to my
   site, [MaxPower](http://www.maxpower.ca) and see [this post](http://www.maxpower.ca/wordpress-hack-creating-a-dynamic-sticky/2005/04/23/)
   for a full explanation.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/using-loops-and-query_posts/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-loops-and-query_posts/page/2/?output_format=md)

The topic ‘Using loops and query_posts’ is closed to new replies.

 * 16 replies
 * 7 participants
 * Last reply from: [Jay](https://wordpress.org/support/users/topdownjimmy/)
 * Last activity: [20 years, 2 months ago](https://wordpress.org/support/topic/using-loops-and-query_posts/page/2/#post-193143)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
