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?
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?
I think this should do it:
query_posts(array('post__in'=>get_option('sticky_posts')));
does get_option('sticky_posts') return a comma separated list of all posts that have the sticky option selected?
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".
Thank you Otto42, it worked :-)
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
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.
oh man, this looks like a lot of new, cool functions to diddle with *sigh*
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?
lairdf might have to resort to the is_sticky conditional tag.
Thank you MichaelH, I'll definitely try!
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.
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?
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 :)
Incorporated some of the ideas from this thread into:
http://codex.wordpress.org/Template_Tags/query_posts#Sticky_Post_Parameters
@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.
wprabbit: Did you solve your issue?
I have the same problem: if there's no sticky post the loop is returning 10 regular posts.
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.
}
?>Thanks a lot, this help me !
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!
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?
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?
`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?
This topic has been closed to new replies.