• 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)
  • 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

    (@clarke1866)

    I’ll give it a whirl, that sounds like a much better idea than what I had in mind! Will post results

    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.)

    It just never makes sense for me why to make a post if you don’t want to show it to anyone?

    You need something like:

    <?php query_posts('p=-1');?>

    You need something like:

    <?php query_posts('p=-1');?>

    Which currently doesn’t exist. Just didn’t want to confuse the initial poster.

    What do you mean it doesnt exist? Where does it not exist ?

    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 = “.

    Is that true for cats as well? or is cat=-2 Ok?

    Yes, cat does have special handling for a single exclusion.

    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.

    Thanks for that.

    Most welcome.

    Yes, cat does have special handling for a single exclusion.
    … though currently imperfect special handling:-)

    Thread Starter clarke1866

    (@clarke1866)

    Hey all, I wrote up why I wanted to use two loops and skip a post. Head to my site, MaxPower and see this post for a full explanation.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Using loops and query_posts’ is closed to new replies.