Forums

Excluding sticky posts using query_posts() (5 posts)

  1. philpeter
    Member
    Posted 2 months ago #

    I had posted this on another thread but it's now been marked resolved without an answer to my query...

    I want to exclude the sticky posts from the loop and the previous thread had this solution;

    query_posts(array(
    "post__not_in"	 =>	get_option("sticky_posts")
    ));

    I've used this solution but it's not working for me. I'm using it in the second loop in my home.php file, which is preceded by wp_reset_query().

    The first loop uses this wp_query:
    query_posts(array('post__in'=>get_option('sticky_posts')));

    The second uses:
    query_posts(array("post__not_in"=>get_option("sticky_posts")));

    But I see the same (sticky) post as the first post produced by both loops. In date order it would be the third post.

    Any idea why this wouldn't work?

  2. MichaelH
    moderator
    Posted 2 months ago #

    I had posted this on another thread but it's now been marked resolved without an answer to my query...

    It's probably better you started another topic anyway...

    As for ignoring sticky posts, the query_posts() article shows

    query_posts('caller_get_posts=1');
  3. philpeter
    Member
    Posted 1 month ago #

    Yep, but caller_get_posts only returns sticky posts to the normal chronological order. I want all sticky posts to be excluded from the loop, which the query_posts() article says is possible by using

    query_posts(array(
    "post__not_in"	 =>	get_option("sticky_posts")
    ));

    Problem is, that isn't working!

  4. philpeter
    Member
    Posted 1 month ago #

    According to @wefixwp, the WP documentation on this is wrong.

    To get around it, I've produced a normal, all-inclusive loop and wrapped all the HTML code within it this IF statement:
    if (!is_sticky())

    This stops the loop actually writing out the post if it is a sticky post. I guess it works but that documentation needs cleaning up.

    Phil

  5. MichaelH
    moderator
    Posted 1 month ago #

    This returned every post except for sticky posts:

    <?php
    $myposts = get_posts(array(
    'post__not_in' => get_option("sticky_posts"),
    'showposts' => -1
    ));
    echo "<pre>"; print_r($myposts); echo "</pre>";
    ?>

Reply

You must log in to post.

About this Topic