Strange Things With Sticky Posts
October 22nd, 2008 by Michael • WordPress Hacks • 6 Comments
Frank wrote in one of his last posts about the new sticky functionality in WordPress 2.7. Right now I'm working on a special theme and I noticed some strange things with sticky posts.
Wrong Post Count
If you like to show 6 posts on a page, I use query_posts('showposts=6');. But if one or more posts are "sticky", they get added to the other 6 posts. To show 6 posts actually, you have to know how many sticky posts you have.
The solution:
get_option('sticky_post') gives you an array back with sticky post ID, which we can count.
$sticky = count(get_option('sticky_posts')); query_posts('showposts='. ( 6 - $sticky ));
Still showing sticky posts in chronological sort order
Pay attention to the CSS classes of the posts. The template tag post_class() gives the post the class. In the case of sticky post, it is also the class "sticky". If you think you can give the class sticky a red background and black border to have a nice teaser you will be disappointed. If you go to the next page to see previous posts, the sticky post will also be showed in the chronological order with a red background and black borders. Not really cool.
Kill the Sticky
If you don't want to have sticky posts, just write in your template:
update_option('sticky_posts', array());
With the empty array you overwrite the sticky options and no sticky posts are availably.








Thank you! Just what I needed and worked like a charm!
What about making two querys? One containing only the sticky posts and another using
caller_get_posts=1to get your posts excluding the sticky ones.Post & Page Parameters
Yes Andreas. That cller_get_post was imolemented after this post. The wrong count is fixed with the release of 2.7 too