WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] "Sticky" Qualifier for query_posts (23 posts)

  1. nathanrice
    Member
    Posted 5 years ago #

    Does anyone know of a way to use query_posts to pull only posts marked as "Sticky" using the new sticky feature in 2.7?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    I think this should do it:
    query_posts(array('post__in'=>get_option('sticky_posts')));

  3. nathanrice
    Member
    Posted 5 years ago #

    does get_option('sticky_posts') return a comma separated list of all posts that have the sticky option selected?

  4. nathanrice
    Member
    Posted 5 years ago #

    Nevermind ... it seems like it returns an array of Post IDs for posts that have been marked sticky.

    This new feature needs some serious documentation. The possibilities of the sticky options are much greater than just "sticking a post to the homepage".

  5. @mercime
    Volunteer Moderator
    Posted 5 years ago #

    Thank you Otto42, it worked :-)

  6. lairdf
    Member
    Posted 5 years ago #

    Thank you Otto42, I've got that example working to fetch only sticky post.

    Now, on the same page (in a different loop), I'd like to fetch posts that are NOT sticky. So, I'd like to exclude those marked as sticky.

    My front page has a feature box at the very top that I'd like to designate as the sticky spot, and then, I have separate blocks for the posts within each category. Right now, the sticky-spot is working great, thanks to your suggestion, but I can't figure out a way to exclude sticky posts from the others.

    Specifically, I have code that looks somewhat like this:

    <?php query_posts('showposts=3&cat=6'); ?>
    <?php while (have_posts()) : the_post(); ?>
    ... stuff ...
    <?php endwhile; ?>

    I'm wondering how I can modify my query_posts so that I can exclude those marked as sticky

  7. MichaelH
    Member
    Posted 5 years ago #

    I'm wondering how I can modify my query_posts so that I can exclude those marked as sticky

    The Excluding Sticky Posts from Custom Queries section of Migrating Plugins and Themes to 2.7 explains that you just need to add caller_get_posts=1 to your query string.

  8. mores
    Member
    Posted 5 years ago #

    oh man, this looks like a lot of new, cool functions to diddle with *sigh*

  9. lairdf
    Member
    Posted 5 years ago #

    My understanding of "caller_get_posts=1" means that it still fetches ALL posts with the category, it just doesn't bring the sticky to the top.

    So, if I have ...
    <?php query_posts('caller_get_posts=1&showposts=3&cat=6'); ?>
    ... it will still bring back the sticky posts.

    So, it sounds like that's the fix for me, but it's really not.

    Help?

  10. MichaelH
    Member
    Posted 5 years ago #

    lairdf might have to resort to the is_sticky conditional tag.

  11. lairdf
    Member
    Posted 5 years ago #

    Thank you MichaelH, I'll definitely try!

  12. reececonrad
    Member
    Posted 5 years ago #

    lairdf, did you figure it out? I'm trying to do the exact same thing. can't get a query returned that excludes the sticky posts.

  13. wprabbit
    Member
    Posted 5 years ago #

    I am working on a new site and wanted to use the new sticky function to create a highlight area ("Items to watch"). Otto's solution works great to pull the sticky posts out, but if there are no sticky posts, it is returning the regular stream of posts and spits out the number of them defined in the settings panel.

    anyone have a way to degrade something like this in case there are no posts specified as sticky?

  14. jordiromkema
    Member
    Posted 5 years ago #

    I used:

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

    to get rid of all the sticky posts in the loop. Strange that it's not in the documentation. Worked for me, hope it works for you too :)

  15. MichaelH
    Member
    Posted 5 years ago #

    Incorporated some of the ideas from this thread into:
    http://codex.wordpress.org/Template_Tags/query_posts#Sticky_Post_Parameters

  16. MichaelH
    Member
    Posted 5 years ago #

    @nathanrice

    The possibilities of the sticky options are much greater than just "sticking a post to the homepage".

    If you have more examples or ideas, please add them to the Codex and thanks for doing so.

  17. jteg
    Member
    Posted 5 years ago #

    wprabbit: Did you solve your issue?

    I have the same problem: if there's no sticky post the loop is returning 10 regular posts.

  18. jteg
    Member
    Posted 5 years ago #

    Sometimes asking a question is just what it takes to solve the problem.

    <?php
    if (get_option('sticky_posts')) {
    
    // Do this if there is any sticky post.
    
    }
    
    else {
    
    // Do this if there isnt.
    
    }
    ?>
  19. chico203
    Member
    Posted 5 years ago #

    Thanks a lot, this help me !

  20. milet22
    Member
    Posted 4 years ago #

    thank you jordiromkema and nathan!

    nathan's definite guide to sticky posts has been invaluable, and jordironmkema, the post_not_in code elegantly replaces a hacked block of code.

    Thanks!

  21. pumpkindoodle
    Member
    Posted 4 years ago #

    I'm having a different problem, using Atahualpa, where I have query_posts to include only one category on homepage:

    <?php
    if (is_home()) {
    query_posts("cat=4");
    }
    ?>

    but when I use that, the formatting I've applied to sticky posts disappears. Atahualpa folks say it's not their issue. Any thoughts?

  22. termserv
    Member
    Posted 4 years ago #

    I'm bumping this.

    I want to use the loop as I usually would, except I'm using query_posts to get the posts from a specific category. How can I use query_posts and still get the sticky posts at the top?

  23. philpeter
    Member
    Posted 4 years ago #

    `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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags