WordPress.org

Ready to get started?Download WordPress

Forums

Using loops and query_posts (17 posts)

  1. clarke1866
    Member
    Posted 9 years ago #

    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.

  2. ColdForged
    Member
    Posted 9 years ago #

    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;

  3. clarke1866
    Member
    Posted 9 years ago #

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

  4. ceo
    Member
    Posted 9 years ago #

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

  5. moshu
    Member
    Posted 9 years ago #

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

  6. Root
    Member
    Posted 8 years ago #

    You need something like:

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

  7. ColdForged
    Member
    Posted 8 years ago #

    You need something like:

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

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

  8. Root
    Member
    Posted 8 years ago #

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

  9. ColdForged
    Member
    Posted 8 years ago #

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

  10. Root
    Member
    Posted 8 years ago #

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

  11. ColdForged
    Member
    Posted 8 years ago #

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

  12. Root
    Member
    Posted 8 years ago #

    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.

  13. Root
    Member
    Posted 8 years ago #

    Thanks for that.

  14. ColdForged
    Member
    Posted 8 years ago #

    Most welcome.

  15. ifelse
    Member
    Posted 8 years ago #

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

  16. clarke1866
    Member
    Posted 8 years ago #

    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.

  17. topdownjimmy
    Member
    Posted 8 years ago #

    Is there any way to do this while only showing one post? In other words, I want to show only the most recent post that doesn't have a particular post-ID.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.